[ Android ] DAY-4 Activity Lifecycle (★★★)

Posted: January 16, 2011 in Android


In some OS, such as Window. When you want to run a program, before you close the program, the program still is running. There, the program is closed by the user or programmer who define the time to close the program. But Android still like that way to stop its program, or having other way?

1. The first thing, is we should know what is Activity.

In SAMS book ( Teach yourself in 24 hours), We find that” The Activity class is central to every Android application. Much of the time, you’ll define and implement an activity for each screen in your application.” Each screen is most important key word.

Activity is the centre component of Android program. Now, let’s see the relations with each other.

There, as we can see, the Activity just a subset of Context . (Maybe, you will ask what does context used for? The context is used to show the program on your screen.)

2. The basic function of cell phone is call. So, the calling is the highest priority. If a call coming, the system should pause the running program. If the phone’s bettery is lower, the system will pause or stop the rest program which is waiting at foreground. So specify to the Activity, its life depends on the system, not user or programmer.

Now, let’s go to our topic. At first, we should understand an Activity Lifecycle diagram.

From the diagram, we can see the 3 colors which are represented “Start”, “running”, “shut down” and “killed”. When we want to run an Activity, the activity will start firstly.  When the activity running, the user can see it (the activity which stated just now) on the screen. If right now, another new activity starts or comes (such as a tip box shows up), the user still can see the last activity, but cannot interact with, there is Pause. If the last activity is hidden by the new activity, cannot see the screen (there, means the old activity is completely covered by the new activity) any more,  it’s stop. If the activity is killed by the system or never started, it’s killed.

3. How does Android control or manage the Activities?

The Android system using  Activity stack to manage them. For example, there are 4 layers in the stack.

A, B, C, D.

The running activity is the highest priority activity which is on the top of the stack. When the new activity comes, the top activity will go to the second layer. If still can see it (the second activity), it’s pause. If cannot see, it’s stop. If the phone doesn’t have enough space or resource to keep the second activity exist, it’s gonna be killed (normally, the lowest one is closed first) . All of layers obey above.

4. Maybe you still have some questions want to ask. As we know, the android phone screen can be fliped. It means the activity will adapt the new screen size. So, how does activity work now?

Okay, actually, the Android will pause the activity, then stop it, and destroy it. Next, it will creat agian, start and resume the activity.

From these, we can see that, the activity life is not managed by user, but Android system.

Thank for reading.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s