1. Part 3
Android
Applications Life Cycle
Notes are based on:
Unlocking Android
by Frank Ableson, Charlie Collins, and Robi Sen.
ISBN 978-1-933988-67-2
Manning Publications, 2009.
Android Developers
http://developer.android.com/index.html
2. 3. Android Application's Life Cycle
Outline
Android Applications
Activity, Service, Broadcast receiver, Content provider
process
Applications Life Cycle
Activity Stack
Life Cycle States
Life Cycle Events
Applications Lifetime: Visible Lifetime Foreground Lifetime
Life Cycle Methods: onCreate(), onStart(), onDestroy()...
Example: LifeCycleDemo
2
3. 3. Android Application's Life Cycle
Android Applications
M畛t 畛ng d畛ng (application) bao g畛m m畛t ho畉c vi component
動畛c 畛nh ngh挑a trong manifest file c畛a 畛ng d畛ng, g畛m c叩c lo畉i:
1. Activity
2. Service
3. BroadcastReceiver
4. ContentProvider
畛ng d畛ng Android so v畛i Java application:
C滴ng vi畉t 1 class 畛 th畛c hi畛n m畛t c担ng vi畛c no 坦
Kh担ng d湛ng hm main kh畛i t畉o m畛t object thu畛c class 坦
v g畛i ph動董ng th畛c c畛a n坦
T湛y theo lo畉i object (Activity, Service....), Android s畉 g畛i
constructor v qu畉n l箪 v嘆ng 畛i c畛a object 坦
3
5. 3. Android Application's Life Cycle
Android Applications
2. Service
M畛t service kh担ng c坦 giao di畛n ng動畛i d湛ng b畉ng h狸nh 畉nh, thay vo 坦, n坦 ch畉y
畉n t畉i background trong m畛t kho畉ng th畛i gian kh担ng x叩c 畛nh.
C坦 th畛 k畉t n畛i (bind to) v畛i m畛t service ang ch畉y (ho畉c ch畉y service 坦 n畉u n坦
hi畛n ch動a ch畉y).
Trong khi ang k畉t n畛i, ta c坦 th畛 li棚n l畉c v畛i service 坦 qua m畛t giao di畛n m n坦
cung c畉p.
5
9. 3. Android Application's Life Cycle
Applications Life Cycle
M畛t ti畉n tr狸nh Linux dnh cho m畛t 畛ng d畛ng Android 動畛c t畉o ra cho
畛ng d畛ng 坦 khi c坦 m畛t s畛 ph畉n code c畛a 畛ng d畛ng 坦 c畉n ch畉y.
Ti畉n tr狸nh 坦 s畉 t畛n t畉i cho 畉n khi
1. kh担ng c嘆n c畉n 畉n n坦 n畛a, HO畉C
2. h畛 th畛ng c畉n thu h畛i b畛 nh畛 畛 cho c叩c 畛ng d畛ng kh叩c d湛ng.
9
10. 3. Android Application's Life Cycle
Applications Life Cycle
M畛t t鱈nh ch畉t 畉c bi畛t nh動ng cn b畉n c畛a Android l c叩c ti畉n tr狸nh
kh担ng tr畛c ti畉p ki畛m so叩t v嘆ng 畛i c畛a ch鱈nh m狸nh.
Thay vo 坦, s畛 ph畉n c畛a n坦 動畛c h畛 th畛ng quy畉t 畛nh d畛a tr棚n th担ng
tin v畛
1. C叩c ph畉n 畛ng d畛ng hi畛n ang ch畉y,
2. T畉m quan tr畛ng c畛a ch炭ng 畛i v畛i ng動畛i d湛ng
3. H畛 th畛ng c嘆n bao nhi棚u b畛 nh畛.
10
11. 3. Android Application's Life Cycle
Component Lifecycles
C叩c component c畛a 畛ng d畛ng c坦 m畛t v嘆ng 畛i (lifecycle)
1. b畉t 畉u: khi Android t畉o th畛c th畛 c畛a component 畛 叩p 畛ng c叩c
intent
2. k畉t th炭c: khi c叩c th畛c th畛 坦 b畛 h畛y.
3. gi畛a ch畛ng: 担i khi ch炭ng c坦 th畛 active ho畉c inactive, hay trong
tr動畛ng h畛p c畛a c叩c activity- visible ho畉c invisible 畛i v畛i ng動畛i
d湛ng.
Life as an Android Application:
Start Active / Inactive End
Visible / Invisible
11
12. 3. Android Application's Life Cycle
Activity Stack
C叩c activity trong h畛 th畛ng 動畛c qu畉n l箪 b畉ng m畛t activity
stack.
Khi m畛t activity m畛i 動畛c b畉t, n坦 動畛c 畉t tr棚n 畛nh stack
v tr畛 thnh running activity -- activity tr動畛c n坦 n畉m b棚n
d動畛i 畛 trong stack, v s畉 kh担ng hi畛n tr畛 l畉i t畉i foreground
(畛nh stack) cho 畉n khi activity m畛i k畉t th炭c.
N畉u ng動畛i d湛ng nh畉n ph鱈m Back, activity ti畉p theo trong
stack s畉 d畛ch l棚n v chuy畛n sang tr畉ng th叩i ho畉t 畛ng
(active).
12
13. 3. Android Application's Life Cycle
Activity Stack
New Activity Running Activity
New Activity Back button pushed or
started running activity closed
Last Running
Activity
Activity n-1
Activity Stack ...
Previous Activity 3
Activities Removed to
Activity 2 free resources
Activity 1
Figure 1. 13
14. 3. Android Application's Life Cycle
Life Cycle States
An activity has essentially
three states:
1. It is active or running
2. It is paused or
3. It is stopped .
Figure 2. 14
15. 3. Android Application's Life Cycle
Life Cycle States
M畛t activity v畛 cn b畉n c坦 ba tr畉ng th叩i:
1. Active / running: ang 畛 t畉i foreground c畛a mn h狸nh
(tr棚n 畛nh c畛a activity stack), ang ch畉y.
2. Paused: m畉t focus, v畉n 動畛c hi畛n th畛 tr棚n mn h狸nh
nh動ng m畛t activity kh叩c ang n畉m tr棚n n坦 v c叩i
activity m畛i ny ho畉c c坦 n畛n trong su畛t ho畉c kh担ng
ph畛 k鱈n mn h狸nh. M畛t activity 畛 tr畉ng th叩i
paused c坦 th畛 b畛 h畛 th畛ng kill n畉u 畛 t狸nh tr畉ng r畉t
thi畉u b畛 nh畛.
3. D畛ng (stopped) b畛 m畛t activity kh叩c che khu畉t hon
ton. N坦 v畉n gi畛 t畉t c畉 c叩c th担ng tin v畛 tr畉ng th叩i v
member. H畛 th畛ng th動畛ng kill n坦 n畉u c畉n b畛 nh畛 cho
vi畛c kh叩c.
15
16. 3. Android Application's Life Cycle
Applications
Life Cycle
Figure 3. 16
23. 3. Android Application's Life Cycle
Life Cycle Methods
Method: onRestart()
動畛c g畛i ngay tr動畛c khi activity 動畛c kh畛i 畛ng l畉i sau khi 達 b畛
d畛ng (stopped).
onStart() lu担n 動畛c g畛i sau 坦.
Method: onStart()
動畛c g畛i ngay tr動畛c khi activity 動畛c hi畛n tr棚n mn h狸nh (visible).
Ti畉p theo l onResume() n畉u activity l棚n foreground, ho畉c onStop()
n畉u n坦 b畛 che.
23
24. 3. Android Application's Life Cycle
Life Cycle Methods
Method: onResume()
1.動畛c g畛i ngay tr動畛c khi activity b畉t 畉u t動董ng t叩c v畛i ng動畛i
d湛ng.
2.T畉i th畛i i畛m ny, activity n畉m tr棚n 畛nh activity stack v nh畉n
input c畛a ng動畛i d湛ng.
3.Ti畉p theo bao gi畛 c湛ng l onPause().
24
25. 3. Android Application's Life Cycle
Life Cycle Methods
Method: onPause()
1.動畛c g畛i khi h畛 th畛ng chu畉n b畛 chuy畛n sang m畛t activity kh叩c.
2.Ph動董ng th畛c ny th動畛ng d湛ng 畛 ghi c叩c thay 畛i ch動a 動畛c l動u,
d畛ng ho畉t h狸nh v nh畛ng c担ng vi畛c t畛n CPU kh叩c, v.v..
3.N坦 n棚n lm c担ng vi畛c c畛a m狸nh th畉t nhanh v狸 activity ti畉p theo
ph畉i 畛i n坦 k畉t th炭c th狸 m畛i resume 動畛c.
4.Ti畉p theo l onResume() n畉u activity quay l畉i, ho畉c onStop() n畉u
n坦 kh担ng c嘆n 動畛c hi畛n th畛 畛i v畛i ng動畛i d湛ng.
5.Activity trong tr畉ng th叩i ny c坦 th畛 b畛 h畛 th畛ng kill.
25
26. 3. Android Application's Life Cycle
Life Cycle Methods
Method: onStop()
1.動畛c g畛i khi activity kh担ng c嘆n hi畛n th畛 畛i v畛i ng動畛i d湛ng.
2.Vi畛c ny c坦 th畛 x畉y ra khi n坦 b畛 h畛y (destroyed), ho畉c do m畛t
activity kh叩c (c滴 ho畉c m畛i) 達 動畛c resume v che n坦.
3.Ti畉p theo l onRestart() n畉u activity t動董ng t叩c tr畛 lo畉i v畛i ng動畛i
d湛ng, ho畉c onDestroy() n畉u n坦 kh担ng quay l畉i.
4.H畛 th畛ng c坦 th畛 kill activity trong tr畉ng th叩i ny.
26
27. 3. Android Application's Life Cycle
Life Cycle Methods
Method: onDestroy()
1.動畛c g畛i tr動畛c khi activity b畛 h畛y.
2.但y l l畛i g畛i hm cu畛i c湛ng m activity nh畉n 動畛c.
3.N坦 c坦 th畛 動畛c g畛i v狸 activity ang k畉t th炭c (hm finish() c畛a
activity 動畛c g畛i), ho畉c v狸 h畛 th畛ng ang t畉m th畛i h畛y th畛c th畛 hi畛n
t畉i c畛a activity 畛 ti畉t ki畛m kh担ng gian b畛 nh畛.
4.Ta c坦 th畛 ph但n bi畛t hai t狸nh hu畛ng tr棚n b畉ng c叩ch d湛ng ph動董ng
th畛c isFinishing().
5.H畛 th畛ng c坦 th畛 kill activity trong tr畉ng th叩i ny.
27
28. 3. Android Application's Life Cycle
Life Cycle Methods
Killable States c叩c tr畉ng th叩i m h畛 th畛ng kill 動畛c
Activity 畛 tr畉ng th叩i killable c坦 th畛 b畛 h畛 th畛ng k畉t th炭c b畉t c畛 l炭c no
sau khi ph動董ng th畛c tr畉 v畛 m kh担ng th畛c thi th棚m m畛t d嘆ng l畛nh no
trong m達 c畛a activity.
Ba ph動董ng th畛c onPause(), onStop(), v onDestroy() d畉n 畉n tr畉ng
th叩i killable.
onPause() l ph動董ng th畛c duy nh畉t 畉m b畉o 動畛c g畛i tr動畛c khi ti畉n
tr狸nh b畛 h畛y (killed) onStop() v onDestroy() c坦 th畛 kh担ng 動畛c g畛i.
Do 坦, ta n棚n d湛ng onPause() 畛 l動u d畛 li畛u c畉n gi畛 l畉i (ch畉ng h畉n
c叩c s畛a 畛i c畛a ng動畛i d湛ng).
28
29. 3. Android Application's Life Cycle
Life Cycle Methods
As an aside
Android Preferences
Preference l m畛t c董 ch畉 g畛n nh畉 畛 l動u tr畛 v 畛c c叩c c畉p key-value thu畛c
c叩c ki畛u d畛 li畛u c董 b畉n. N坦 th動畛ng 動畛c d湛ng 畛 l動u c叩c preference c畛a 畛ng
d畛ng, ch畉ng h畉n nh動 l畛i cho m畛ng m畉c 畛nh ho畉c m畛t font ch畛 c畉n n畉p khi
畛ng d畛ng 動畛c b畉t l棚n.
G畛i Context.getSharedPreferences() 畛 畛c v ghi c叩c gi叩 tr畛.
G叩n t棚n cho t畉p preference c畛a m狸nh n畉u ta mu畛n d湛ng ch炭ng t畉i c叩c
component kh叩c trong c湛ng m畛t 畛ng d畛ng, ho畉c d湛ng
Activity.getPreferences() kh担ng c坦 t棚n 畛 d湛ng ri棚ng cho activity ang g畛i.
Ta kh担ng th畛 chia s畉 preference cho c叩c 畛ng d畛ng kh叩c (tr畛 khi d湛ng content
provider). 29
30. LAYOUT
3. Android Application's Life Cycle
Example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
Life Cycle android:id="@+id/myScreen"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff000000"
>
<TextView
Example android:layout_width="fill_parent"
android:layout_height="wrap_content"
畛ng d畛ng sau tr狸nh di畛n android:text="@string/hello"
/>
m畛t s畛 t狸nh hu畛ng <EditText
android:id="@+id/txtColorSelect"
chuy畛n tr畉ng th叩i android:hint="Background color (red, green, blue)"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
x畉y ra trong m畛t chu </EditText>
<TextView
tr狸nh s畛ng c畛a m畛t android:id="@+id/txtToDo"
android:layout_width="fill_parent"
activity i畛n h狸nh. android:layout_height="wrap_content"
android:background="#00000000">
<!-- transparent -->
</TextView>
<Button
android:text=" Finish "
android:id="@+id/btnFinish"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
30
31. 3. Android Application's Life Cycle
Example: Life Cycle
Code: Life Cycle Demo. Part 1
package es.demo;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
//GOAL: show the following life-cycle events in action
//protected void onCreate(Bundle savedInstanceState);
//protected void onStart();
//protected void onRestart();
//protected void onResume();
//protected void onPause();
//protected void onStop();
//protected void onDestroy();
31
32. 3. Android Application's Life Cycle
Example: Life Cycle
Code: Life Cycle Demo. Part 2
private LinearLayout myScreen;
private TextView txtToDo;
private EditText txtColorSelect;
private Button btnFinish;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myScreen = (LinearLayout) findViewById(R.id.myScreen);
txtToDo = (TextView) findViewById(R.id.txtToDo);
String msg = "Instructions: n "
+ "0. New instance (onCreate, onStart, onResume) n "
+ "1. Back Arrow (onPause, onStop, onDestroy) n "
+ "2. Finish (onPause, onStop, onDestroy) n "
+ "3. Home (onPause, onStop) n "
+ "4. After 3 > App Tab > re-execute current app n "
+ " (onRestart, onStart, onResume) n "
+ "5. Run DDMS > Receive a phone call or SMS n "
+ " (onRestart, onStart, onResume) n "
+ "6. Enter some data - repeat steps 1-5 n ";
txtToDo.setText(msg); 32
33. 3. Android Application's Life Cycle
Example: Life Cycle
Code: Life Cycle Demo. Part 3
txtColorSelect = (EditText) findViewById(R.id.txtColorSelect);
// you may want to skip discussing the listener until later
txtColorSelect.addTextChangedListener(new TextWatcher(){
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
changeBackgroundColor(s.toString());
}
});
btnFinish = (Button) findViewById(R.id.btnFinish);
btnFinish.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
finish();
}
});
Toast.makeText(getApplicationContext(), "onCreate", 1).show();
}
33
37. 3. Android Application's Life Cycle
Example: Life Cycle
Code: Life Cycle Demo. Part 7
private static String MYPREFSID; //used in part 6
private static int actMode; //used in part 6
private void changeBackgroundColor (String theChosenColor){
// change background color
if (theChosenColor.contains("red"))
myScreen.setBackgroundColor(0xffff0000);
else if (theChosenColor.contains("green"))
myScreen.setBackgroundColor(0xff00ff00);
else if (theChosenColor.contains("blue"))
myScreen.setBackgroundColor(0xff0000ff);
else {
//reseting user preferences
clearMyPreferences();
myScreen.setBackgroundColor(0xff000000);
}
}
37
38. 3. Android Application's Life Cycle
Example: Life Cycle
Code: Life Cycle Demo. Part 8
/*
protected void onRestoreInstanceState(Bundle savedInstanceState)
This method is called after onStart() when the activity is being re-initialized
from a previously saved state.
The default implementation of this method performs a restore of any view state
that had previously been frozen by onSaveInstanceState(Bundle).
Ph動董ng th畛c ny 動畛c g畛i sau onStart() khi activity ang 動畛c kh畛i t畉o l畉i t畛
tr畉ng th叩i 達 l動u l畉i tr動畛c 坦. Ci 畉t m畉c 畛nh c畛a ph動董ng th畛c ny kh担i ph畛c
tr畉ng th叩i view 達 動畛c l動u b畛i onSaveInstanceState(Bundle).
*/
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Toast.makeText(getBaseContext(),
"onRestoreInstanceState ...BUNDLING",
Toast.LENGTH_LONG).show();
}
38
39. 3. Android Application's Life Cycle
Example: Life Cycle
Code: Life Cycle Demo. Part 9
/*
protected void onSaveInstanceState(Bundle outState)
Called to retrieve per-instance state from an activity before being killed
so that the state can be restored in
onCreate(Bundle) or onRestoreInstanceState(Bundle)
(the Bundle populated by this method will be passed to both).
This method is called before an activity may be killed so that when it comes
back some time in the future it can restore its state. For example, if activity B
is launched in front of activity A, and at some point activity A is killed to
reclaim resources, activity A will have a chance to save the current state of
its user interface via this method so that when the user returns to activity A,
the state of the user interface can be restored via:
onCreate(Bundle) or onRestoreInstanceState(Bundle).
Ph動董ng th畛c ny 動畛c g畛i tr動畛c khi m畛t activity c坦 th畛 b畛 kill sao cho khi n坦 quay l畉i
n坦 c坦 th畛 ph畛c h畛i tr畉ng th叩i c畛a m狸nh. V鱈 d畛, khi activity A b畛 h畛 th畛ng kill 畛 l畉y
ti nguy棚n, A s畉 c坦 c董 h畛i l動u tr畉ng th叩i hi畛n hnh c畛a giao di畛n ng動畛i d湛ng b畉ng
ph動董ng th畛c ny, 畛 khi ng動畛i d湛ng quay l畉i activity A, tr畉ng th叩i c畛a giao di畛n ng動畛i
d湛ng c坦 th畛 動畛c kh担i ph畛c qua c叩c ph動董ng th畛c onCreate(Bundle) ho畉c
onRestoreInstanceState(Bundle). (畛i t動畛ng Bundle m ph動董ng th畛c ny x但y d畛ng s畉 動畛c
truy畛n cho c畉 hai ph動董ng th畛c kh担i ph畛c 坦)
39
*/
40. 3. Android Application's Life Cycle
Example: Life Cycle
Code: Life Cycle Demo. Part 10
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Toast.makeText(getBaseContext(),
"onSaveInstanceState ...BUNDLING",
Toast.LENGTH_LONG).show();
} // onSaveInstanceState
}//LifeCycleDemo
40
41. 3. Android Application's Life Cycle
Example: Life Cycle
onCreate onStart onResume
41
42. 3. Android Application's Life Cycle
Example: Life Cycle
onPause onStop onDestroy
After pressing Back Arrow
42