1

Explain the life cycle of an activity, in android?

  • Below cycle is always true.
    onCreate-> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy()
  • Below cycle executes always.
    onCreate-> onStart() -> onRestoreInstanceState() -> onResume() ->
    onSaveInstanceState() -> onPause() -> onStop() -> onDestroy()
  • Below cycle executes always.
    onCreate-> onStart() -> onResume() -> onStop() -> onPause -> onDestroy()
  • Below cycle executes always.
    onCreate-> onStart() -> onRestoreInstanceState() -> onResume() ->
    onPause() -> onSaveInstanceState() -> onStop() -> onDestroy()

Answer: A Below cycle is always true.
onCreate-> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy()
Description: Explanation for android activity life cyle:
onCreate()
1.The system calls this when creating your activity.
2.You should initialize the essential components of your activity.
Eg:setcontentview
3.Using findviewbyid(), to programmatically interact with widgets in the UI
4.calling managedQuery
5.You can call finish()
6.If you want any thread running in the background to download DATA, you can do it here. This has to be deleted in onDestroy()
7.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

onStart()
1.Called after onCreate(Bundle) or after onRestart() followed by onResume().
2.you can register a BroadcastReceiver in onStart() to monitor changes that impact your UI, You hav to unregister it in onStop()
3.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
onResume()

1.Called after onRestoreInstanceState(Bundle), onRestart(), or onPause()
2.begin animations, open exclusive-access devices (such as the camera)
onPause ()

1.Called as part of the activity lifecycle when an activity is going into the background
2.The counterpart to onResume().
3.B will not be created until A's onPause() returns. (So don’t do anything long here)
4.Save all your persistent data here.
5.Good place to do things like stop animations (any CPU consuming things), to make switching to other activity as soon as possible.
6.Close your camera here if it is opened
7.Eg: if device goes to sleep or some dialog is displayed, then it will go to onPause().
8.onStop() will follow this function, once the other waiting activity resumes and started displaying on the scree. This is not guaranteed as in some cases onResume() will be called with out onStop().
9.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
onStop ()

1.Called when you are no longer visible to the user
2.You will next receive either onRestart(), onDestroy()
3.this method may never be called, in low memory situations
4.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
onDestroy ()

1.Perform any final cleanup before an activity is destroyed
2.do not count on this method being called as a place for saving data!
3.This method is usually implemented to free resources like threads that are associated with an activity,
4.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown
onRestart ()

1.Called after onStop() when the current activity is being re-displayed to the user
2.It will be followed by onStart() and then onResume()
3.If you have deactivated any cursor in onStop(), call managedQuery() again.
4.Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
onSaveInstanceState – this is where we have to save transient state of the application
onRestoreInstanceState – this is where we have to restore the transient state of our application that has been crashed due to low memory scenarios.

2

In Activity's onCreate function, What is the purpose of super.oncreate() ?

  • It gives permission to use your phone screen
  • it calls super class constructor
  • it does nothing , simply a return statement will be there in super class
  • it will create graphical window for sub class.

Answer: D it will create graphical window for sub class.
Description: Every activity should inherit from Activity class.

Activity base class provides 3 basic functions for every screen:
1.It provides empty graphical window on which you can load your designed screen.
2.Activity Base Class will handle all the UI events which are not handled by programmer.
3.Activity Base Class will handle transition states of your activity, when you are moving from one activity to other activity.

super.onCreate:
When you call super.onCreate it will provide empty graphical window on which you can load your screen.
If you comment super.oncreate(); then it will crash your program because there is no container to load your screen.

3

Is it mandatory to implement oncreate() & onstart() of activity life cycle ? Will it run if those life cycle methods are removed?

  • it gives compile time error
  • run time exception, supernotcalled
  • nothing will happen it will run perfectly
  • your phone will hang

Answer: C nothing will happen it will run perfectly
Description: It is a standard, that programmer has to implement corresponding life cycles methods of activity based on the functionality supported. But one can skip those functions if programmer wish to.

4

Is it possible to have an Activity without UI?

  • not possible
  • yes, if it is doing some functionality with out UI

Answer: B yes, if it is doing some functionality with out UI
Description: Generally every activity will have UI (User Interface) screens. But if programmer wants he can omit UI and do some background functionality with an Activity.
Note: To do background functionality its better to use service than an activity with out UI. It makes more sense.

5

How to start an activity to get response? Or How to start an activity to get result?

  • startActivityOnResult()
  • startActivityForResult()
  • startActivity()
  • setResult()

Answer: B startActivityForResult()
Description: startActivityForResult(Intent intent) is the function to use to start an activity, which returns some result to you on finishing itself.

Start Activity for Result():
We will use this function if we are expecting some result from child activity.
Start Activity for result will take two parameters:
1.Intent.
2.Integer Request code (it is a integer constant)
Returning Results form ChildActivity:
If you want to return success to the parent activity then use below functions.
setResult(RESULT_OK); and then finish();
Once you set the result then you have to immediately call “finish” function.
To return failure:
setResult(RESULT_CANCELED); and then finish();
In case if child activity crashes due to some reason then it will automatically pass RESULT_ CANCELED.
How to catch the results from the child in the parent activity:
For this we have to implement a function called onActivityResult(……) in parent activity.
OnActivityResult() function has 3 paramaters
1.Request code
2.Result code
3.Intent.

6

Activity life cycle: When LCD goes off, what is the life cycle function gets called in activity?

  • i. onPause()
  • ii. onStop()
  • iii. onSaveInstanceState()
  • i
  • ii
  • i & ii
  • i, ii, & iii

Answer: A i
Description: When lcd goes off, activity will be moved to onPause() state. This is a special case.

7

Activity life cycle: When a new activity comes on top of your activity completely, then what is the life cycle function that gets executed in the old activity?

  • it calls onPause()
  • it calls onPause() -> then -> onStop()
  • it calls onPause() -> then -> onStop() -> onDestroy()
  • it calls onPause() -> then -> onSaveInstanceState()

Answer: B it calls onPause() -> then -> onStop()
Description: When new activity comes on top of an existing activity, then existing activity will move to invisible state by calling onPause() -> then -> onStop. If top activity is transparent or doesn't occupy complete screen, then below activity's onStop() will not be called.

8

Activity life cycle: When a dialog is displayed on top of your activity, is your activity in foreground state or visible state?

  • it calls onPause()
  • it calls onPause() -> then -> onStop() -> onDestroy()
  • it calls onPause() -> then -> onStop()
  • it calls onResume()

Answer: A it calls onPause()
Description: When a dialog comes on top of an existing activity, then existing activity will move to partially invisible state by calling onPause().

9

start an activity with startactivityforresult(), and the child activity gets crashed. Now what is the result code obtained by the parent?

  • RESULT_OK
  • RESULT_CANCELLED
  • RESULT_CRASH
  • RESULT_FINISH

Answer: B RESULT_CANCELLED
Description: In case of crash, parent activity will get RESULT_CANCELLED.

10

What are the data storage options in android? Note: persistent data.

  • i. files, arrays, database
  • ii. files, databses
  • iii. using network servers
  • i
  • ii
  • i & iii
  • ii & iii

Answer: D ii & iii
Description: Persistent data means permanent data, so use files, databases, sharedpreferences, and network servers to store persistent data. Arrays can hold only temporary data.

11

Android ANR(Application Not Responding) : What is the UI response time limit in android. (I.E with in how many seconds main thread has to respond to user actions?)

  • 5 sec
  • 10 sec
  • 1 sec
  • 2 sec

Answer: A 5 sec
Description:If user touches your activity, and if your program doesn't respond to user events with in 5 seconds, android will not tolerate it and it force closes your application. this error is called as ANR (Application Not Responding).

12

What is the intent-filter of main activity that launches your application's main screen?

  • <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  • <intent-filter>
    <action android:name="Intent.MAIN" />
    <category android:name="Category.LAUNCHER" />
    </intent-filter>
  • It will not have any intent-filter by default.
  • <intent-filter>
    <action android:name="android.intent.action.LAUNCHER" />
    <category android:name="android.intent.category.MAIN" />
    </intent-filter>

Answer: A <intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Description: action.MAIN - says this activity is the main activity (starting point) for this application.
category.LAUNCHER - says this activity should appear in the home screen's launcher.

13

How to create UI (user interface) without using xml file? Show with one example on how to create a button without having xml file?

  • @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
  • @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button b;
    setContentView(b
    }
  • @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button b = new Button(this);
    setContentView(b);
    }
  • @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button b = findViewById(R.id.button1);
    setContentView(b);
    }

Answer: C @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button b = new Button(this);
setContentView(b);
}
Description:

14

Activity life cycle: What is the mandatory activity life cycle function that will get called in case of configuration changes?

  • onStop()
  • onPause()
  • onSaveInstanceState()
  • both 2 and 3

Answer: D both 2 and 3
Description: In case of low memory or configuration changes, android will call both onPause() and onSaveInstanceState() with out fail. Exception: there is one exceptional case, that is if programmer is handling configuration changes, then in that case it will not call those functions.

15

Activity life cycle onStop: Can I save all my databse table updates in onStop() of activity? If not explain why and also explain where should I save db tables?

  • Yes we can.
  • No, save it in onSaveInstanceState
  • No, because onStop will never be called
  • No, because onStop() may not be called in some situations.

Answer: D No, because onStop() may not be called in some situations.
Description: In case of low memory or configuration changes, there is a chance that android may force-kill your application before reaching onStop(). onPause() is the only function that will be called with out fail before killing the application. so save all persistent data like DB tables in onPause() only.
Note : We can't save all database tables in onSaveInstanceState, because that function will not be called if user presses back button.

16

What is difference between persistent data and transient data?

  • Persistent data is temporary data, eg: creating an array in the program.
  • transient data is temporary data, that we store in database tables
  • Persistent data is permanent data that we store, eg in database tables, and transient data is logical data that we use in programming logic.
  • reverse of option 3

Answer: C Persistent data is permanent data that we store, eg in database tables, and transient data is logical data that we use in programming logic.
Description: Persistent data means permanent, and transient means temporary.

17

What will happen if super.oncreate() from oncreate() function of activity is commented?

  • Compile time error
  • time exception, super not called.
  • Nothing will happen, it will still execute with empty window
  • nothing will happen, it will execute with proper output

Answer: B Run time exception, super not called.
Description: All activity's life cycle functions have to call super class methods, as super class Activity will do lot of helping functionalities for derived class. If you don't call it will throw run time exception.

18

What is the difference between this context and getapplicationcontext ? which one to use when?

  • no difference, both are same
  • this points to entire process, appcontext points to current class.
  • this points to current context, application context points to entire process. if your context is of entire life time of process then use app context, else this.
  • both option 2 and 3 are correct

Answer: C this points to current context, application context points to entire process. if your context is of entire life time of process then use app context, else this.
Description: this pointer always points to current class object, app context will point to entire process. there is only one app context. if you want to use some control whose life time is through out your application life time then go for app context, else use this pointer (activity context).

19

Where to register broadcast receivers that updates UI? If I have a broadcast receiver that updates my UI frequently, then where should I register that broadcast receiver in my activity life cycle functions?

  • oncreate()
  • onstart()
  • onresume()
  • onsaveinstancestate()

Answer: B onstart()
Description: onStart() function will be called just before your activity will be made visible to the user. So any thing that affects UI has to be registered in onStart() function.

20

Save image in onsaveinstatncestate: A heavy image downloaded from internet in onCreate() of an activity. Is it possible to save it in onsaveinstancestate() in case of configurationChanges?

  • yes
  • no, we have to save it in onPause()
  • no, we have to use some static reference to it.
  • no, we don't need to save it at all.

Answer: C no, we have to use some static reference to it.
Description: Generally configuration changes causes activity to get destroyed and recreated. So if you have downloaded heavy image from internet in onCreate() function, then there is a chance that it will be re downloaded in onCreate() one more time if user rotates the phone. To avoid this we can have a static reference to that image to avoid multiple downloads.