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.