Top 3 Android Interview Questions #2 (Android Developer)

If you are an Android experienced developer and want to prepare for Android Interview Questions. There are common Android interview questions that can make confuse you during an interview in top companies. Here we are sharing some common question which can help you crack the android interview. Read it and for more details, you can find sources of android application development document links in this post.

Top 3 Android Interview Questions Android Developer

Let’s Start Android Interview Questions Part #2

1.  What is the Intent filter?

Answer: the Intent filter is used for what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type while filtering out those that are not a means for the component. A Simple example if your application is a PDF reader then you have to define specific subelements,<action><category>, and <data>.

Here is simple code intent filter to receive an ACTION_SEND intent when the data type is text:

<activity android:name="ShareActivity">
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="text/plain"/>
    </intent-filter>
</activity>

* can we create a more intent filter for single Activity, Broadcast receiver or service?

You can create a filter that includes more than one instance of <action>, <data>, or <category>. If you do, you need to be certain that the component can handle any and all combinations of those filter elements.

Source: https://developer.android.com/guide/components/intents-filters

Bouns: Know about more ” Intent Filters in Android

2. What are Launch Mode and its types?

Answer: The launchMode the attribute specifies an instruction on how the activity should be launched into a task. There are four different launch modes you can assign to the launchMode attribute:

"standard" Default. The system always creates a new instance of the activity in the target task and routes the intent to it.

"singleTop" If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() a method, rather than creating a new instance of the activity.) One task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity)

"singleTask" The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to it’s onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

"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.

Source : https://developer.android.com/guide/topics/manifest/activity-element#lmode.

3. What is the relationship between the lifecycle of an AsyncTask and the lifecycle of an Activity?  

What problems can this result in, and how can these problems be avoided?

Answer:  An AsyncTask is not tied to the lifecycle of the Activity that contains it.

– If you start an AsyncTask inside an Activity and you rotate the device, the Activity will be destroyed and a new instance will be created.

– Similarly, if user navigates to another activity, current activity will be destroyed or go in background activity stack and new activity would be in the foreground.

But the AsyncTask will not die. It will go on living until it completes. There will be many problems in this case

  • When it completes, the AsyncTask won’t update the UI of the new Activity. Indeed it updates the former instance of the activity that is not displayed anymore. This can lead to an Exception of the type java.lang.IllegalArgumentException
  • On the long run, this produces a memory leak: if the AsyncTask lasts for long, it keeps the activity “alive” whereas Android would like to get rid of it as it can no longer be displayed. The activity can’t be garbage collected and that’s a central mechanism for Android to preserve resources on the device.

The solution is to avoid using AsyncTasks for long-running background tasks or you may call the cancel() method but if it is not handled in doInBackground() method.

Bonus: Know more about ” Android AsyncTask example with a progress bar in kotlin


Bouns: “More Android Interview Questions”

This is 3 most frequently or common Android interview questions asked in an interview with the Android developer. This android interview questions could be for both beginner and experienced android developer.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.