Android life cycle

fasthall 6,784 views 17 slides Nov 05, 2012
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

No description available for this slideshow.


Slide Content

Android Life Cycle
Wei-Tsung Lin

Introduction
●Android is a mobile operating system.
●There are many interrupts during normal
using, such as a call.
●Also, most desktop users have been familiar
with multi-task operating system, expecting
that they can browsing the web while
listening to the musics.
●However, the memory is not unlimited in
mobile devices.
●So, Android introduced "Life Cycle".

Activity
●In Android, each application is a process,
called activity.
●Android (aka Dalvik VM) maintains an
unique activity stack to observer the status
of process, and manager the memory.
●Life cycle of an activity is managed by
Android OS, not by application itself.

Status of an activity
●Active
●Paused
●Stopped
●Dead

Active
●"Active" is the status that an activity is
running.
●There will be only one "Active" activity. The
others are int the status of "Pause",
"Stopped", and "Dead".

Paused
●"Paused" means an activity is in the
background.
●Creating toast, dialog, or receiving a phone
call will make an running activity paused.
●User cannot interact with an activity which is
paused.

Stopped
●The activity has already exit the screen, no
other action is running.
●We can wake an stopped activity up by
using "Notification" or multi-task button.

Dead
●The activity has been finished manually, or
garbage collected by system.

Low memory
●When memory is running out, Dalvik will
follow the recycle rule to release memory.

1.Independent Activity/Service/Intent Receiver
2.Stopped Activity (by LRU)
3.Service
4.Visiable Activity
5.Running Activity

Activity API
●OnCreate()
●OnStart()
●OnResume()
●OnPause()
●OnStop()
●OnRestart()
●OnDestroy()

ANR
●Android will prompt you a notification that
"Application Not Responding" when an
activity executing without response for a long
time.
●Default timeout is 5 seconds.
●To avoid that, developers shouldn't execute
a long time task in the main thread, such as
accessing Internet, database, or playing
music.

AsyncTask
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress ((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}

protected void onProgressUpdate(Integer... progress) {
setProgressPercent (progress[0]);
}

protected void onPostExecute(Long result) {
showDialog ("Downloaded " + result + " bytes");
}
}

Service
●Four components in Android: Activity,
Service, BroadcastReceiver,
ContentProvider
●Except ContentProvider, the others main
objects are running in the main thread.
●The life cycle of Service is most persistent
(longest).
●System will kill a paused activity in very high
possibility.

Service
●Putting a long time task into a thread is not
enough.
●Consider rotating your device. Actually the
activity is killed, and then system recreate a
new activity rotated. The thread will be killed
too!
●You should not only put the long time task
into a new thread, but also a Service.
●Although the original activity is killed, the
service will execute as usual. You can make
inter-process communication later.

MessageQueue
●Android implement a message queue to
handle messages, which called Looper.
●It's very helpful when you want to update UI
outside the main thread(UI thread).
●A handler can push a message into a looper,
dispatch, and handler the message in the
queue.
●When creating a handler, it will bind to the
looper of current activity automatically. Also,
you can assign a specific looper to bind.

IntentService
●IntentService is actually a Service.
●Using IntentService, you don't need to
implement Looper, Message, and Handler
yourself.

Intent intent = new Intent(MainActivity.this, MyIntentService.class);
intent.putExtra("tag", "message");
startService(intent);

in MyIntentService:

@Override
protected void onHandleIntent(Intent intent) {
String message = intent.getStringExtra("tag");
}

Reference
http://developer.android.
com/training/basics/activity-lifecycle/index.html
http://developer.android.
com/guide/components/services.html
http://developer.android.
com/reference/android/os/Handler.html
http://developer.android.
com/reference/android/os/Looper.html
http://developer.android.
com/reference/android/app/IntentService.html
Tags