A curated collection of Android interview questions and answers
- Core Java or Kotlin
- Core Android
- Design
- Data Storage
- Architecture
- Tools and Libraries
- Test Driven Development
- Others
- What is difference between
Serializable
andParcelable
? Which is best approach in Android? - What is the difference between
Service
andIntentService
? How is each used? - What is the context ? How is it used?
When we have more than one method with the same name in a single class but the arguments are different, then it is called as method overloading.
Overriding concept comes in picture with inheritance when we have two methods with same signature, one in parent class and another in child class. We can use @Override
annotation in the child class overridden method to make sure if parent class method is changed, so as child class.
Serializable
is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations.
Parcelable
is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient than Serializable, and to get around some problems with the default Java serialization scheme.
Service
: It is the base class for the Android services, that you can extend for creating any service. Since the service run inside the UI thread, it requires that you create a working thread for executing its work.
IntentService
: it is a subclass of Service, that simplifies your work. It works already in a working thread, and can receive asynchronous requests. So, you don't need to create it manually, or to worry about synchronization. You can simply extend it and override the method onHandleIntent(Intent intent)
IntentService do for you:
- Creates a default worker thread that executes all intents delivered to
onStartCommand()
separate from your application's main thread. - Creates a work queue that passes one intent at a time to your
onHandleIntent()
implementation, so you never have to worry about multi-threading. - Stops the service after all start requests have been handled, so you never have to call
stopSelf()
- Provides default implementation of
onBind()
that returns null. - Provides a default implementation of
onStartCommand()
that sends the intent to the work queue and then to youronHandleIntent()
implementation.
As the name suggests, it’s the context of the current state of the application/object. It lets newly-created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity and package/application).
And also, Context is a handle to the system, it provides services like resolving resources, obtaining access to databases and preferences, and so on. An Android app has activities. It’s like a handle to the environment your application is currently running in. The activity object inherits the Context object. It allows access to application specific resources and class and information about the application environment.
Wrong use of Context can easily lead to memory leaks in an android application.
There are many different types of context in android, so let’s understand what are those, how to use those and when to use which one.
Application Context
It is an instance which is the singleton and can be accessed in an activity via getApplicationContext(). This context is tied to the lifecycle of an application. The application context can be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of an activity.
Activity Context
This context is available in an activity. This context is tied to the lifecycle of an activity. The activity context should be used when you are passing the context in the scope of an activity or you need the context whose lifecycle is attached to the current context.
getContext() in ContentProvider
This context is the application context and can be used similar to the application context. This can be accessed via getContext() method.
Build types define properties that Gradle uses when building and packaging your Android app.
- A build type defines how a module is built, for example whether ProGuard is run.
- A product flavour defines what is built, such as which resources are included in the build.
- Gradle creates a build variant for every possible combination of your project’s product flavours and build types.
SharedPreference: It stores data in XML files. It is the simplest way to store private data in the key-value pair.
SQLite: It stores structure data in the private database.
Internal Storage: It stores data in the device file system and any other app cannot read this data.
External Storage: Data is stored in the file system but it is accessible to all apps in the device
You should make sure your question/answer is appropriate and unique for this repository and if you don't know how to contribute, you should read CONTRIBUTING guide before pull request.
All projects and packages in this repository are MIT licensed.