Cdacians

Cdacians
Cdacians

Friday 14 September 2012

Android Activity Life Cycle



Android Activity Life Cycle
We had a quick glance on “What an android activity is?” in our previous post, here we are going to discuss about Life cycle involved in an android activity.
Activity in the system is managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity — the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits. It might be little bit confusing, let me explain it with an example:
Assume you open Facebook app in your android mobile, you are looking through the shares/posts posted by your friends and you are coming across one interesting web link posted by your friend. You click on the link to view what is there inside, it takes you to web browser and open up the website. If you look at the android activity stack level, facebook activity is now placed below the browser activity in activity stack and facebook activity will come to foreground only when browser activity is closed. So the state of facebook activity is saved and will be used when it move to top of the stack.
Hope you got the meaning out of what I am trying to say.
Android activity life cycle is controlled by life cycle methods and we will see below what they do:
The onCreate method is called after an Activity instance has been created. This is where most applications perform most of their initialization: reading in the layouts and creating View instances, binding to data, and so on. Note that, if this Activity instance has not been destroyed, nor the process killed, this is not called again. It is called only if a new instance of an Activity class is created. The argument to this method is a Bundle object that contains saved application state. If there is no saved state, the value of this argument is null.

Before you proceed with the code, give a glance at the Android life cycle-State diagram given in the Android developers site. You will understand the inter relationship between different states of an android life cycle.
Take a look at the below program to see what kind of information and in what order the information is logged:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.prgguru.android;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
 
public class ActivityLifeCycleActivity extends Activity {
// Make strings for logging
private final String TAG = this.getClass().getSimpleName();
private final String RESTORE = ", can restore state";
// The string "fortytwo" is used as an example of state
private final String state = "fortytwo";
 
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.main);
String answer = null;
// savedState could be null
if (null != savedState) {
answer = savedState.getString("answer");
}
Log.i(TAG, "onCreate"
+ (null == savedState ? "" : (RESTORE + " " + answer)));
}
 
@Override
protected void onRestart() {
super.onRestart();
// Notification that the activity will be started
Log.i(TAG, "onRestart");
}
 
@Override
protected void onStart() {
super.onStart();
// Notification that the activity is starting
Log.i(TAG, "onStart");
}
 
@Override
protected void onResume() {
super.onResume();
// Notification that the activity will interact with the user
Log.i(TAG, "onResume");
}
 
protected void onPause() {
super.onPause();
// Notification that the activity will stop interacting with the user
Log.i(TAG, "onPause" + (isFinishing() ? " Finishing" : ""));
}
 
@Override
protected void onStop() {
super.onStop();
// Notification that the activity is no longer visible
Log.i(TAG, "onStop");
}
 
@Override
protected void onDestroy() {
super.onDestroy();
// Notification that the activity will be destroyed
Log.i(TAG, "onDestroy "
// Log which, if any, configuration changed
+ Integer.toString(getChangingConfigurations(), 16));
}
 
@Override
protected void onSaveInstanceState(Bundle outState) {
//Save instance-specific state
outState.putString("answer", state);
super.onSaveInstanceState(outState);
Log.i(TAG, "onSaveInstanceState");
}
 
@Override
public Object onRetainNonConfigurationInstance() {
Log.i(TAG, "onRetainNonConfigurationInstance");
return new Integer(getTaskId());
}
 
@Override
protected void onRestoreInstanceState(Bundle savedState) {
super.onRestoreInstanceState(savedState);
// Restore state; we know savedState is not null
String answer = null != savedState ? savedState.getString("answer")
: "";
Object oldTaskObject = getLastNonConfigurationInstance();
if (null != oldTaskObject) {
int oldtask = ((Integer) oldTaskObject).intValue();
int currentTask = getTaskId();
// Task should not change across a configuration change
assert oldtask == currentTask;
}
Log.i(TAG, "onRestoreInstanceState"
+ (null == savedState ? "" : RESTORE) + " " + answer);
}
}
  • Create a new Android project
  • Replace the default activity code with the one given above
  • Make sure your eclipse editor shows Logcat in fast view. If it is not showing, choose logcat [ Windows >> Show view >> Others >> Android >> Logcat ]
  • Run the application using emulator or device.

Now you could see the set of log messages in ‘Logcat’ window. In order to generate interesting logging information, you can start other applications and go back and forth, using the application switcher or the Launcher to return to the Finch application.

Start enough other applications, and on returning to the Finch application,you will see that the process ID, or PID, has changed, but the application appears to be in the same state as you left it. This is because state was restored for this activity, and all other components of the application, from the saved state.
OK, it’s time to dig the log messages:
I just ran the application in my Android device, the log message generated in the Logcat was:
Application which I am seeing in mobile is:
Now I am opening another application[Profiler app], you can open any installed application. We just gonna test how the states behave when we move from one app to another:
See what happened by reading the below log message, our application’s state is saved and application is paused for now.  The application’s state will be reused when we come back to our application:
I closed the Profiler app and returning back to my application:
I pressed ‘Home’ button of the android device, my application is still running:
I re-opened the application as it was below in the android activity stack, now it has been moved to top of the stack. Associated log messages:
I pressed back button which close the application and the corresponding log message:

Thanks
akm
www.cdacians.com

No comments:

Post a Comment