Monday, January 28, 2013

Activity - Android

What is Activity?
It is basic UI with which user can interact
It creates a window for developer,in which we can place our UI controls.

Activity Life Cycle?
In Android, Activities are managed as stack trace.
[Activity is launched]
|
onCreate()
|
onStart()
|
onResume()
|
[Activity is now visible and user can interact] 
|
If any other Activity tries to come on scree
|
onPause()
|
onStop()
|
onDestroy() 

How to start an Activity?
Android provides us functions startActivity() and startActivityForResult()
Example:
Intent intent = new Intent(runningActivity.this, targetActivity.class);
startActivity(intent);

What is the difference between startActivity and startActivityForResult?
Both the methods starts an Activity.
Activity started via startActivityForResult  would return a result to the calling Activity.


What is intent?
Intent is an abstract description of Action to be performed?
or It is an intention to do an Action.
2 type of information is stored in Intent
(a) Action - The general action to be performed.
(b) Data - Data to operate on

Oh!!! My Activity is killed by OS b'coz there was low memory scenario, How to save data so, that it is not lost?
The Android OS can kill the Activities which are in background i.e. user is not interacting with them, so, whenever our Activity is going in the background we need to save the data, which user would expect on screen when he would come back.

For saving the basic data-type, we can use onSaveInstanceState()
For saving the complex data, we can use onSaveNonConfigData()
Example:
onSaveInstanceState(Bundle data) {
 super.onSaveInstanceState(data);
 data.putlong("key",value); //put data here
}


onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 if(savedInstanceState!=null)
 long l = savedInstanceState.getlong("key"); //Retreive data
}

No comments:

Post a Comment