1

What is Manifest file?if we have two java files A.java and B.java in your source folder which one opens first and why?

  • The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code.
    The activity which is having the following Intent Filter will be executed first:
    <intent-filter>
    <action android:name="android.intent.action.FIRST" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  • The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code.
    The activity which is having the following Intent Filter will be executed first:
    <intent-filter>
    <action android:name="android.intent.action.ONE" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  • Manifest files says the capabilities of your application and number of components in your application. The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code.

    The activity which is having the following Intent Filter will be executed first:
    <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
  • None are correct

Answer: C Manifest files says the capabilities of your application and number of components in your application. The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code.
The activity which is having the following Intent Filter will be executed first:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Description:

2

What is Splash screen or splash activity?

  • Splash is a receiver in android that will be triggered after every 10 minutes.
  • Splash is used in camera application for turning on camera's flash.
  • Splash screen is an activity that will be shown generally as the initial screen while starting your application. This screen will be used to showcase your company's logo and other app related and company related basic information. Generally splash screen will be displayed for few seconds before going to main screen of the project.
  • None are correct

Answer: C Splash screen is an activity that will be shown generally as the initial screen while starting your application. This screen will be used to showcase your company's logo and other app related and company related basic information. Generally splash screen will be displayed for few seconds before going to main screen of the project.
Description: A splash screen can be designed by using some alarms or timers, so that main screen will be displayed and splash screen will be destroyed after that many seconds. Eg: Candy Crush game shows a splash screen with "King" in the starting screen before going to the actual screen. While it shows that screen it does some background work also to start next heavy activity.

3

Where do you write intent filters and Why?

  • Intent-Filters are always in AndroidManifest.xml because the Android System uses intent filters, in order to find a suitable component to handle the intent. An Intent Filter defines the intent that a component can respond to.
  • We can have intentfilters in java code also in case of dynamic broadcast receivers.
  • both option 1 and 2 are correct.
  • In styles.xml because android system uses intent filter to determine for which activity which style need to be loaded.

Answer: C both option 1 and 2 are correct.
Description:

4

How to save transient states in activity life cycle? eg: If I am using some variables, which will change based on my activities life cycle, then where should I save those variables, in case of configuration changes?

  • Save all those variables in onSaveInstanceState().
  • Save it using onRetainNonConfigurationInstance function.
  • Save those variables in onPause.
  • Android will save & restore all these variables, so programmer don't need to worry.

Answer: A Save all those variables in onSaveInstanceState().
Description: These kind of variables are called as transient state of Activity. These will not be saved by Android, it is programmers duty. So save it in onSaveInstanceState().

5

Can I use onSaveInstanceState for saving all my transient states?

  • Yes, that function is meant for it.
  • You should not use it for saving very large objects as it might take time to serialize and de-serialize it.
  • option 2 is true, so use it only for saving small transient states.
  • none of the above.

Answer: C option 2 is true, so use it only for saving small transient states.
Description: onSaveInstanceState() function has to be used for saving small objects (transient states). If we want to save large objects use onRetainNonConfigurationInstance() function.

6

Can I save my database updates in onSaveInstanceState() of activity life cycle ?

  • Yes, you can save, but make sure that it will not take too much of time.
  • Yes, we can save database of any size in that function.
  • No, because that function might not be called all the time.
  • none of the above.

Answer: C No, because that function might not be called all the time.
Description: We have to save all DB updates on or before onPause() function of activity life cycle. Because this is the last guaranteed function to be called on all scenarios of an activity life cycle.
Note : If you save DB in onSaveInstanceState, then that function will not be called if user presses back button on the activity.
So it will end up in loosing all the database un saved data.

7

If some heavy images are downloaded from internet in onCreate() of an Activity, then where to save them in case of configuration changes (rotating the phone)?

  • Use a static variable to point to that image, so that it will not be killed on activity death.
  • Use onRetainNonConfigurationInstances() function to save it.
  • Save that image in some file or DB and retrieve it in onCreate().
  • we can use either option 1 or 2.

Answer: D we can use either option 1 or 2.
Description: onSaveInstanceState() function has to be used for saving small objects (transient states). If we want to save large objects use onRetainNonConfigurationInstance() function. Or else we can make that image as static, so that the image will be loaded only once.

More documentation :

onSaveInstanceState() function has to be used for saving small objects, not for heavy objects.

If you want to save heavy images on phone rotation, then use any of below techniques:
1. If you want to save large objects use onRetainNonConfigurationInstance() function.
2. Or else we can make that image as static, so that the image will be loaded only once. Meaning: On downloading an image from network, make it pointed by a static variable. If user rotates the phone, since android kills that activity and recreates it, just put a if condition check if that static variable is not null, then only download again. As you know static variables will be created only once, it will not download again.

But preferably go for 1st option.

8

When starting an activity from a notification, will it start as new task or old task?

  • Neither. it will be started as a system in Notification Manager task.
  • It will be started in the old task, which has given pending intent to Notification Manager
  • it will be started as a new task always.

Answer: C it will be started as a new task always.
Description: When we start a new activity from Notification, it is fresh starting point, so it has to be started as a new task. that's why when we are starting an activity from notification, we have to use FLAG_NEW_TASK in the intent.

9

What does the flag FLAG_ACTIVITY_CLEAR_TOP do here in below android code?

Assume that activities in the stack are A->B->C->D. Code is currently running in activity 'D'. Now what will happen in this scenario?

Intent in = new Intent(this, B.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(in);
  • It will start one more instance of Activity 'B' on top of activity 'D'. So it will look like A->B->C->D->B..
  • It will not launch one more instance of Activity 'B' on top of activity 'D'. Instead this will send a new intent to onNewIntent() function of activity 'B' and B will come on top of D. so it will look like A->C->D->B.
  • It will not launch one more instance of Activity 'B' on top of activity 'D'. Instead this will send a new intent to onNewIntent() function of activity 'B' and Activities C, D will be destroyed automatically.. so it will look like A->B.
  • It will launch new instance of activity 'B' in a different task. so total two tasks are available now. task 1: A->B->C->D and task 2: B.

Answer: C It will not launch one more instance of Activity 'B' on top of activity 'D'. Instead this will send a new intent to onNewIntent() function of activity 'B' and Activities C, D will be destroyed automatically.. so it will look like A->B.
Description: 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 to onNewIntent() function.

10

If activity is forcefully closed by Android system due to screen orientation change, then how to save UI states?

  • Save all your UI stats in onSaveInstanceState().
  • Save all your UI states using onRetainNonConfigurationInstance function.
  • Save all your UI states in onPause.
  • Android will save & restore UI states automatically, so programmer don't need to worry.

Answer: D Android will save & restore UI states automatically, so programmer don't need to worry.
Description: Generally orientation changes will cause android to kill and recreate the activity. But all the UI states will be saved and restored by android automatically. But to do this programmer has to supply id for each view. Other than this programmer don't need to take any extra effort.

Note : Android will save and restore only UI states, not transient states (any variable values).