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.
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.
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.
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.
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.
Answer: A i
Description: When lcd goes off, activity will be moved to onPause() state. This is a special case.
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.
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().
Answer: B RESULT_CANCELLED
Description: In case of crash, parent activity will get RESULT_CANCELLED.
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.
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).
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.
Answer: C @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button b = new Button(this);
setContentView(b);
}
Description:
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.
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.
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.
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.
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).
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.
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.