Site icon C1CTech

Android Interview Questions

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

Exit mobile version