Given below is the list of most commonly asked Android Interview questions
1) What is Android?
Android is an open-source, Linux-based operating system designed primarily for touchscreen mobile devices such as smartphones and tablets but now it is also used in television,watch, game consoles, digital cameras, PCs and other electronics.
2) Explain Android application components.
Following is a list of components of Android application architecture:
- Activities : An activityis the entry point for interacting with the user. It represents a single screen with a user interface.
- Services : It is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface.
- Broadcast receivers : A broadcast receiver is a component that enables the system to deliver events to the app outside of a regular user flow, allowing the app to respond to system-wide broadcast announcements.
- Content providers : used to share data between the applications.
3) What are the code names of android?
Version | Code name | API Level |
1.5 | Cupcake | 3 |
1.6 | Donut | 4 |
2.1 | Eclair | 7 |
2.2 | Froyo | 8 |
2.3 | Gingerbread | 9 and 10 |
3.1 and 3.3 | Honeycomb | 12 and 13 |
4.0 | Ice Cream Sandwitch | 15 |
4.1, 4.2 and 4.3 | Jelly Bean | 16, 17 and 18 |
4.4 | KitKat | 19 |
5.0 | Lollipop | 21 |
6.0 | Marshmallow | 23 |
7.0 | Nougat | 24-25 |
8.0
9.0 |
Oreo
Pie |
26-27
28 |
4) What are the advantages of Android?
Open-source : It means no license, distribution and development fee.
Platform-independent : It supports Windows, Mac, and Linux platforms.
Supports various technologies : It supports camera, Bluetooth, wifi etc. technologies.
Highly optimized Virtual Machine : Android uses a highly optimized virtual machine for mobile devices, called DVM (Dalvik Virtual Machine).
5) What are the core building blocks of android?
The core building blocks of Android are:
- Activity
- View
- Intent
- Service
- Content Provider
- Fragment etc.
6) What is activity in Android?
An activity is the entry point for interacting with the user. It represents a single screen with a user interface.
7) What are the life cycle methods of android activity?
There are 7 life-cycle methods of activity. They are as follows:
- OnCreate() : fires when the system first creates the activity. Here we can write basic application startup logic , instantiate some class-scope variables , get data from bundles , associate the activity with a ViewModel etc.
- OnStart() : Called when the activity is becoming visible to the user. Here the app initializes the code that maintains the UI.
- OnResume() : Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
- OnPause() : Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed.
- OnStop() : Called when your activity is no longer visible to the user.
- OnDestroy() : Called when the activity is finishing using finish() or configuration change.
- OnRestart() : Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). It will be followed by onStart() and then onResume().
8) What is intent?
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a Background Service. The primary pieces of information in an intent are:
- action : The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
- data : The data to operate on, such as a person record in the contacts database, expressed as a Uri.
9)Difference between implicit and explicit intent?
Explicit Intents
Explicit Intents are used to call a specific component. When you know which component you want to launch and you do not want to give the user free control over which component to use.
For example, you have an application that has 2 activities. Activity A and activity B. You want to launch activity B from activity A. In this case you define an explicit intent targeting activityB and then use it to directly call it.
Implicit Intents
Implicit Intents are used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use.
For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.
10) Explain the use of ‘bundle’ in android?
Bundles are generally used for passing data between various Android activities.
Pass data between activities by using Bundle and Intent objects.
Bundle b = new Bundle();
b.putString("myname", anystring);
Intent in = new Intent(getApplicationContext(), secondActivity.class);
in.putExtras(b);
startActivity(in);
//In the second activity, we have to access the data passed from the first activity
Intent in = getIntent();
//Now, you need to get the data from the bundle
Bundle b = in.getExtras();
//Finally, get the value of the string data associated with key named "myname"
String s = b.getString("myname");
11)What is Application?
The Application class in Android is the base class within an Android app that contains all other components such as activities and services. Application or its sub classes are instantiated before all the activities or any other application objects have been created in Android app.
12)What is Context?
A Context is an Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
13) Describe content providers
A ContentProvider provides data from one application to another, when requested. It manages access to a structured set of data. It provides mechanisms for defining data security(i.e. by enforcing read/write permissions). ContentProvider offer a standard interface that connects data in one process with code running in another process.
When an application wants to access the data of a ContentProvider, it makes a request. These requests are handled by the ContentResolver object, which communicates with the ContentProvider as a client. The provider object receives data requests from clients, performs the requested action, and returns the results in cursor format.
14) How to access data using Content Provider?
1.To retrieve data from a provider, your application needs “read access permission” for the provider and must be defined in manifest.
2.Then, get access to the ContentResolver object by calling getContentResolver() on the Context object, and retrieving the data by constructing a query using ContentResolver.query().
3.The ContentResolver.query() method returns a Cursor, so you can retrieve data from each column using Cursor methods.
15) Describe services.
A Service is an application component that can perform long-running operations in the background, and it continues to run in the background even if the user switches to another application. These are the three different types of services:
- Foreground Service: A foreground service performs some operation that is noticeable to the user. For example, we can use a foreground service to play an audio track. Foreground services must display a Notification.
- Background Service: A background service performs an operation that isn’t directly noticed by the user. In Android API level 26 and above, there are restrictions in using background services and it is recommended to use WorkManager in these cases.
- Bound Service: A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results. A bound service runs only as long as another application component is bound to it.
16)Difference between service and thread?
Service: A service is simply a component that can run in the background, even when the user is not interacting with your application, so you should create a service only if that is what you need.
Thread: If you must perform work outside of your main thread, but only while the user is interacting with your application, you should instead create a new thread.
17) Difference between Service & Intent Service
- Service is the base class for Android services that can be extended to create any service. A class that directly extends Service runs on the main thread by default so it will block the UI (if there is one) and should therefore either be used only for short tasks or should make use of other threads for longer tasks.
- IntentService is a subclass of Service that uses a worker thread to handle all of the start requests, one at a time. This is the best option if you don’t require that your service handle multiple requests simultaneously. It stops itself when it runs out of work.
18) Launch modes in Android?
There are four different launch modes you can assign to the launchMode attribute:
- Standard: (the default mode) It creates a new instance of an activity in the task from which it was started. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.For Eg:
Suppose there is an activity stack of A -> B -> C.
Now if we launch B again with the launch mode as “standard”, the new stack will be A -> B -> C -> B.
- SingleTop: It is the same as the standard, except if there is a previous instance of the activity that exists in the top of the stack, then it will not create a new instance but rather send the intent to the existing instance of the activity.For Eg:
CASE 1:
Suppose there is an activity stack of A -> B.
Now if we launch C with the launch mode as “singleTop”, the new stack will be A -> B -> C as usual.
CASE 2:
Now if there is an activity stack of A -> B -> C.
If we launch C again with the launch mode as “singleTop”, the new stack will still be A -> B -> C.(Here old instance gets called and intent data route through onNewIntent() callback)
- SingleTask: A new task will always be created and a new instance will be pushed to the task as the root one. So if the activity is already in the task, the intent will be redirected to onNewIntent() else a new instance will be created. At a time only one instance of activity will exist.For Eg:
CASE 1:
Suppose there is an activity stack of A -> B -> C
Now if we launch D with the launch mode as “singleTask”, the new stack will be A -> B -> C -> D as usual.
CASE 2:
Now if there is an activity stack of A -> B -> C -> D.
If we launch activity B again with the launch mode as “singleTask”, the new activity stack will be A -> B.
Activities C and D will be destroyed.
- SingleInstance: Same as “singleTask“, except that the system doesn’t launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.For Eg:
CASE 1:
Suppose there is an activity stack of A -> B -> C . If we launch activity D with the launch mode as “singleInstance”, the new activity stack will be:
Task1 — A -> B -> C
Task2 — D (here D will be in different task)
CASE 2:
Suppose you have A, B, C activities in one task and activity D is in another task with “launch mode = singleInstance”. Now you again launching activity D-
State of Activity Stack before launch D
Task1 — A -> B -> C
Task2 — D
State of Activity Stack after launch D activity
Task1 — A -> B -> C
Task2 — D (Here old instance gets called and intent data route through onNewIntent() callback)
19) Mention two ways to clear the back stack of Activities when a new Activity is called using intent
The first approach is to use a FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK in conjunction.
The second way is by using FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_NEW_TASK in conjunction.
20) What’s the difference between FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_CLEAR_TOP?
FLAG_ACTIVITY_CLEAR_TASK :
If set,this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
FLAG_ACTIVITY_CLEAR_TOP :
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK.
Hope you find this useful. This is just a list of questions I personally found useful in interviews.