It is important to master the life cycle of an activity, because a normal Android application, there will be a lot of activities, how to switch between these activities, data interaction, etc., will often use the activity of the life cycle of this piece of knowledge. It can be said that as long as the activity of the life cycle, to more smoothly write Android programs.
Tools/Materials
A. Return stack
- 1
Stack we all know that one of the most basic data structures. Has a last-in-first-out feature. Like a bottle, we put ping-pong in it, the last table tennis must be at the top of the bottle, so, when we want to take table tennis from the bottle, the last ping pong will be taken out first, this is the most basic stack
- 2
Activity is managed using tasks, and a task is a set of activities that exist in the stack, which is the return stack.
It means that there will be multiple tasks in an Android system, and one task will have a return stack.
- 3
So, Android activity is cascading, each start a new activity, the activity will be added to the top of the stack, become topactivity, covering the original topactivity, click the Back button will destroy the top activity, And one of the following activities will be re-topactivity.
END
Two. Activity status
- 1
There are 4 states of activity:
1) operation status;
2) suspend state;
3) Stop State;
4) Destruction Status
- 2
1) Operating Status
When an activity is at the top of the stack that returns the stack, it is in the running state. The system is not going to recycle the running state of the activity. Ask, you are playing a game, the results of the system for you to recycle, and then back to the homepage, you are certainly uncomfortable.
- 3
2) Pause Status
If I start a new activity now, but this activity is not full screen, so you can still see the next activity from the free space nearby, and the activity on the back is suspended. Activities that are in a paused state are generally not recycled. Because the user may quickly click the Back button to return to this activity. Ask, if you in the current main activity pop up a dialog box, you choose to finish the return, found that the interface is not, in the mind is certainly uncomfortable. Of course, if the memory is really low and low, the system will consider recycling. But now the cell phone memory is very large, it is rarely seen in this situation.
- 4
3) Stop State
When an activity is completely invisible, it is in a stopped state. For example, when you start a second activity in an activity, the first activity is stopped. Activity in the stopped state, his data and status is unreliable, because the system may reclaim this piece of data. But don't worry, the data is actually saved by code,
- 5
4) Destruction Status
When an activity is removed from the return stack, for example, I press the return key, the activity is removed. At this point, the activity is in the destruction state, the system is very happy to destroy this piece of data, of course, is also very happy to the system to destroy this piece of data. Otherwise, the activities are not destroyed, how much resources to occupy Ah, the mobile phone quickly knelt.
END
Three. Activity Lifetime--7 callback methods
- 1
The activity has 7 methods, namely:
1) onCreate ();
2) OnStart ();
3) Onresume ();
4) OnPause ();
5) onStop ();
6) OnDestroy ();
7) Onrestart ();
- 2
1) onCreate ()
This method is called when the activity is created for the first time. Note that it was created for the first time. This method will not be called if the activity is re-entered from the stopped state to a running state.
- 3
2) OnStart ()
This activity is called when it is visible by invisible entry. For example, when entering from the stop state to the running state is called. For example, an activity that opens a dialog box and then presses the return key to re-enter the event will not be invoked.
- 4
3) Onresume ()
Called when the activity is ready to interact with the user. For example, many students are racing together now, the teacher said, the next group to run to the starting line, ready to run. This is Onresume (), at this time the activity must be located at the front desk, students must stand on the runway, a truth.
- 5
4) OnPause ()
The current activity needs to be invoked to start another activity, or to resume another activity. In this method, we can release some CPU resources, because when you are not in the foreground, in the pause or stop or even destroy the state, you should let out some resources, reduce the burden of the system.
- 6
5) OnStop ()
Called when the activity is completely invisible. For example, the current activity initiates a dialog-style event, so OnStop () is not executed. But when you start a full-screen activity, you get the call.
- 7
6) OnDestroy ()
This is very well understood, needless to say, the activity has been destroyed, press the return button, or finish, etc.
- 8
7) Onrestart ()
Called by the stop state to a running state. If the paused state becomes running, it will not be called.
END
Four. Illustrative examples
-
Now write an Android program, the specific program code will not write. This program mainly has the following functions:
1) main activity, there are 2 buttons, one is to start a new Acvititya, and the other is to start a dialog box-style activityb;
2) Add a new Activitya, is full-screen;
3) Add a new Activityb, is the dialog box;
4) The seven major callback functions of the re-active activity.
-
Simply write down the code here.
How do I create a dialog-box activity?
Very simply, modify the registration of the activity in the manifest file to
<activity
Android:name= "Com.example.activitytest.DialogActivity"
Android:label= "@string/app_name"
Android:theme= "@android: Style/theme.dialog" >
</activity>
Added Android:theme= "@android: Style/theme.dialog"
-
Modify the code for the main activity, overriding the 7 callback functions
-
OK, now let's click on the icon to start the application;
found that 3 functions were called.
-
At this point, click the Back button, exit the program, called the function.
-
Start another activity in this program.
-
Return to this activity in the second activity
-
Now, start another dialog-box activity.
-
And then back to this activity.
END
Four. Life cycle of activities
- 1
It can be seen from the above that activities are divided into;
Full lifetime: A total of 6 methods were called between the OnCreate () method and OnDestroy ().
Visible lifetime: The activity is between the OnStart () method and OnStop (), a total of 4 methods, plus a rerun of the Onrestart () method, totaling 5.
Foreground lifetime: Active in the Onresume () method and the OnPause () method, a total of 2 methods.
Life cycle of Android activity