Android Activity Life Cycle
First condition is :-
User launches an application having one single activity, and after
successful launch of the activity, user presses the back button of the device. So, in this situation the following methods of Activity Class will participate in lifecycle process that gets called for the launch of the activity are :
onCreate(Bundle saveState){ }
onStart(){ }
onResume(){ }
Now user can see and interact with the activity.
After that
user presses the back button of the device. Now in that case activity is
going to be killed by Android system and all the resources deallocation
process will be done.
Methods called for this are:
onPause(){ }
onStop(){ }
onDestroy(){ }
Second Condition is :-
In second condition user launches the application by pressing the icon.
The activity is on the front of user. So for this following methods gets
called :
onCreate(Bundle saveState){ }
onStart(){ }
onResume(){ }
Now the activity is visible to user and user can interact with the activity.
Now there is a button on the first
activity. By pressing the button user will be navigated to second
activity. So in this condition android system will call following
methods for the first activity :
onPause(){ }
onStop(){ }
Now user is on second activity.
After that from second activity user
presses the back button of the device,so user will be navigated back to
first activity. Now while again displaying the activity on the screen
android will call following methods for the first activity :
onRestart(){ }
onStart(){ }
onResume(){ }
As now user is on first activity so when he again presses the back button following methods gets called :
onPause(){ }
onStop(){ }
onDestroy(){ }
Third Condition is:-
In third condition user presses the application icon to launch the
application. This time our second activity is transparent. On the first
activity there is a text and a button on the activity. On launch of
first activity following methods get called :
onCreate(Bundle saveState){ }
onStart(){ }
onResume(){ }
Now the activity is visible to user and user can interact with the activity.
Now there is a button on the
first activity. By pressing the button user will be navigated to second
activity. As already mentioned the second activity is transparent so
this time following methods gets called for the first activity :
onPause(){ }
Now user is on second activity. User can
also see the UI content of the first activity behind the second activity
but he can not interact with the components of activity.
After being on second activity now user presses the back button. So he will again move back to the first activity.
This time following methods gets called for the first activity :
After being on second activity now user presses the back button. So he will again move back to the first activity.
This time following methods gets called for the first activity :
onResume(){ }
As now user is on first activity. So when he again presses the back button following methods gets called :
onPause(){ }
onStop(){ }
onDestroy(){ }
Additional Condition:
We have one additional condition, which is user launches the application by pressing the application icon, and user can see the first launching activity. The methods gets called will be :
onCreate(Bundle saveState){ }
onStart(){ }
onResume(){ }
Now this time user presses the Home button of the device. So, in this conditions the methods gets called will be :
Now this time user presses the Home button of the device. So, in this conditions the methods gets called will be :
onPause(){ }
onStop(){ }
This time if android system had enough memory, then the instance of activity will not be destroyed.
Now user again launches the application. Now system will call following methods for the activity :
This time if android system had enough memory, then the instance of activity will not be destroyed.
Now user again launches the application. Now system will call following methods for the activity :
onRestart(){ }
onStart(){ }
onResume(){ }
As now user is on activity. So when he again presses the back button following methods gets called :
onPause(){ }
onStop(){ }
onDestroy(){ }
Question1:
When we rotate our device , which methods of activity are called?
when configuration (such as a change in screen orientation, language, input devices, etc)changes Android destroys and recreates current activity unless we handled Orientation changes. When orientation changes following life cycle methods get called methods gets called :
protected void onSaveInstanceState(Bundle outState)
protected void onStop()
protected void onDestroy()
public void onCreate(Bundle savedInstanceState)
protected void onStart()
protected void onRestoreInstanceState(Bundle savedInstanceState)
protected void onResume()
Why these methods are called twice?protected void onStop()
protected void onDestroy()
public void onCreate(Bundle savedInstanceState)
protected void onStart()
protected void onRestoreInstanceState(Bundle savedInstanceState)
protected void onResume()
Because any application resource, including layout files, can change based on any configuration value. Thus the only safe way to handle a configuration change is to re-retrieve all resources, including layouts, drawables, and strings. Because activities already know how to save their state and re-create from that state, this is a convenient way to have an activity restart itself with a new configuration.
Do Not set the screen orientation in the manifest (android:screenOrientation ="portrait"). Add the android:configChanges="keyboardHidden|orientation", and override your onConfigurationChanged(). This way your onCreate will not be called twice.
Here a example of your onConfigurationChanged:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig); // tem que ter
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}
}
Question2:
When is the savedInstanceState bundle actually used?
it's used when the Activity is forcefully terminated by the OS (ex: when your Activity is in the background and another task needs resources).
When this happens, onSaveInstanceState(Bundle bundle) will be called and it's up to your app to add any state data you want to save in bundle.
When the user resumes your Activity, onCreate(Bundle savedInstanceState) gets called and savedInstanceState will be non-null if your Activity was terminated in a scenario described above. Your app can then grab the data from savedInstanceState and regenerate your Activity's state to how it was when the user last saw it.
Basically in onCreate, when savedInstanceState is null, then it means this is a 'fresh' launch of your Activity. And when it's non-null (if your app saved the data in onSaveInstanceState(...), it means the Activity state needs to be recreated. activities already know how to save their state and re-create from that state.
In Other Words
onSaveInstanceState is used to store data only for application lifetime (i.e. temporarily)
The data is held in memory only until the application is alive, in other words this data is lost when the application closes, so in your case when you force close the app onSaveInstanceState is not used.
It can only be called when you do operations while your application is still alive, for e.g. when you change the screen orientation the activity is still intact so onSaveInstanceState is called.
However if you want to permanently store the data you would have to use SharedPreferences and SQLite database.
**IMPORTANT NOTE :-
Calling of onStop(){ } method is not sure every time during the lifecycle process of an activity. Its Behavior changes with availability of device memory.If Device is in Critical Low memory condition then it just overpasses the onStop(){ } method and directly calls the onDestroy() { } method directly after onPause(){ } method call. Thats Why it is Recommended that you should do Application closing time operation in onPause(){ } & not in onStop(){ }.
"Anyone is free to comment with his own condition regarding activity."
created
No comments:
Post a Comment