Android Technical Interview Questions Flashcards
What is a ContentProvider & what is it used for?
A ContentProvider manages access to a structured set of data. It encapsulates the data & provides mechanisms for defining security. ContentProvider is the standard interface that connects data in one process with code in another process.
List the methods in the Activity Lifecycle
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onRestart()
- onDestroy()
Describe a scenario where onPause() & onStop() would not be called before onDestroy() in the activity lifecycle.
if finish() is called from within the onCreate() method.
Why should you not rely on onDestroy() to manage resource destruction?
While onDestroy() is the last callback within the Activity Lifecycle, it may not always be called - instead, create & destroy resources in onStart(), onResume and onPause(), onStop() respectively.
Describe 3 common cases for using an Intent.
1) To start an Activity via startActivity()
2) To start a Service via startService()
3) To deliver a broadcast via sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast()
What is an Intent? Can it be used to provide data to a ContentProvider? why or why not?
An intent is a common mechanism for starting a new Activity and transferring data between Activities - you cannot start a ContentProvider via an Intent. To access data in a ContentProvider, you must use the ContentResolver object via app Context to communicate with the provider as a client.
What is the difference between an Activity and a Fragment?
Activity: A since, focused operation that a user can perform (e.g. dial a #, take a picture, send an email, etc) - however, there is nothing preventing the implementation of a more complex Activity
Fragment: A modular section of an Activity with its own lifecycle & input events which can be added/removed at will. A Fragment’s lifecycle is directly affected by its host Activity’s lifecycle.
What is the difference between Serializable & Parcelable? Which is the best approach in Android?
Serializable - A standard Java interface that automatically serializes a marked class in certain situations
Parcelable - An Android-specific interface where you implement the serialization yourself
Parcelable was created to be far more efficient than Serializable & work around some issues with the default Java Serialization scheme.
Is it possible to create an Activity in Android w/o a UI?
Yes, these activities are treated as abstract activities
Which method is called only once in a Fragment lifecycle?
onAttached()
What is a Broadcast Receiver?
A Broadcast Receiver communicates w/ the OS for things such as “check whether an internet connection is available”
What is ANR & why does it happen?
ANR - Application Not Responding; means the UI thread is likely held up or the Activity is in onResume() while the user is interacting with the Activity.
This occurs due to starting heavy & long tasks on the main UI thread, rather than starting them in the background via AsyncTask
What is the difference between Service & IntentService?
Service - The base class for Android services that can be extended to create any service. Any class that extends Service runs on the main thread (& will block the UI if there is one) & therefore, should only be used for short tasks or use other threads for longer tasks
IntentService - Subclass of Service that handles async requests on demand. Writing an IntentService can be quite simple - just extend the IntentService class & override onHandleIntent(intent) where you can manage all incoming requests.
What are launch modes?
Launch modes are the way in which a new instance of an Activity is to be associated w/ the current task
What are the two mechanisms by which launch modes can be defined?
Launch modes may be defined using the AndroidManifest file or using Intent Flags that declare if & how the new Activity should be associated with the current task.
What is the relationship between the lifecycle of an AsyncTask & an Activity?
An AsyncTask is not tied to the lifecycle of an Activity that contains it.
What problems can arise from the relationship between an AsyncTask & an Activity?
If you start an AsyncTask inside an Activity & that Activity is destroyed, the AsyncTask will continue on until it completes, resulting in:
- Updates to the former instance of the Activity, rather than the new one (leading to an Exception)
- Potential memory leak since the AsyncTask maintains a reference to the Activity
How can the problems from the relationship between an AsyncTask & an Activity be avoided?
The problems can be avoided by using a different mechanism for long-running background tasks (e.g. Service)
Suppose you start a service in an Activity where the service accesses a remote server via internet connection. If the Activity is showing an animation that indicates some kind of progress, what issue might you encounter & how could you address it?
If a delayed response occurs, it could present that app as seeming to be frozen & locked up, or even worse - hang up the entire UI thread.
To address this, the network call should be done on a background thread via asynchronous mechanism.
In an app you’re working on, you notice that a view’s value is not being restored after screen reorientation. What could be a likely cause of the problem that you should verify at a minimum, about that view?
Verify that the view has a valid ID (via android:id attributes)