when users need to interact with the phone through the screen, such as making a phone call, taking a photo, sending an email, or viewing a map. Developers need to implement an activity. Each activity is used as a window to provide the user with an interface. It can fill the entire screen, or it can be smaller, larger than the screen, or even floating on top of other windows. As one of the four most important components in Android apps. Today we will learn about the life cycle of activity in Android .
First, create an Android project that contains two Activitya and Activityb
10 kinds of situation discussion
First, the normal start activity A, the output results are as follows:
Second, jump from Activitya to Activityb, and Activityb fullscreen. That is, the Activitya is completely covered by the ACTIVITYB and the output is as follows:
Thirdly, in the second case, returning from Activityb to Activitya, the output is as follows:
Four, jump from Activitya to Activityb, and activityb not full screen. That is, the Activitya is not completely covered by the ACTIVITYB, the output is as follows:
Five, in the case of the fourth kind, return from Activityb to Activitya, the output is as follows:
Six, in the first case, click the Home button, the output is as follows:
VII, in the sixth case, long press the home keyboard, from the task Manager to bring up the Activitya, the output is as follows:
In the first case, click the Power key and the output is as follows:
Ninth, in the case of the eighth type, when the Power key is turned back to Activitya, the output is as follows:
Tenth, exit Activitya, the output is as follows:
after discussing the above 10 kinds of situations, it may feel a little complicated, in fact, it is not complicated, in carding. First, the easiest thing to find is that OnCreate and OnDestroy were executed only once from beginning to end. OnCreate executes once when an activity first starts, and OnDestroy executes once when the activity is closed. It can also be found that except in the case of 452, the results of the remaining cases are the same, leaving activity a, then a executes onpause and OnStop, and when a is returned, the Onrestart, OnStart, and Onresume are executed one time. so we can draw the following conclusions. As long as it does not enter into other activities that are not full-screen, a executes onpause and onstop, whereas only one onpause is executed, returns from other activities, a executes Onrestart, OnStart, and onresume, whereas Onresume is executed.
When testing 452 cases, simply add the following line of code to the Androidmanifest.xml Squadron ACTIVITYB node:
Android:theme= "@android: Style/theme.dialog"
Attached Activity Activitya Code:
[Java]View Plaincopy
- Package Com.yuxianglong.view;
- import android.app.Activity;
- import android.content.Intent;
- import Android.os.Bundle;
- import Android.util.Log;
- import Android.view.View;
- import Android.widget.Button;
- /**
- *
- * @ClassName: Activitya
- * @Description: Testing the activity life cycle, overloading the Oncreate,onstart,onresume,
- * Onstop,onpause
- * Seven Methods of Ondestroy,onrestart
- * @author Gyz
- * @date 2014-7-9 a.m. 10:55:37
- *
- */
- Public class Activitya extends Activity {
- Private Button button;
- Private String Tag = "yuxlong2010";
- @Override
- protected void onCreate (Bundle savedinstancestate) {
- Super. OnCreate (savedinstancestate);
- LOG.D (Tag, "I ' m onCreate");
- Setcontentview (r.layout.activity_a);
- Button = (button) Findviewbyid (R.id.button);
- Button.setonclicklistener (new View.onclicklistener () {
- @Override
- Public void OnClick (View v) {
- //TODO auto-generated method stub
- StartActivity (new Intent (Activitya. this, activityb. class));
- }
- });
- }
- @Override
- protected void OnStart () {
- //TODO auto-generated method stub
- Super. OnStart ();
- LOG.D (Tag, "I ' m OnStart");
- }
- @Override
- protected void Onresume () {
- //TODO auto-generated method stub
- Super. Onresume ();
- LOG.D (Tag, "I ' m Onresume");
- }
- @Override
- protected void onStop () {
- //TODO auto-generated method stub
- Super. OnStop ();
- LOG.D (Tag, "I ' m onStop");
- }
- @Override
- protected void OnDestroy () {
- //TODO auto-generated method stub
- Super. OnDestroy ();
- LOG.D (Tag, "I ' m OnDestroy");
- }
- @Override
- protected void Onrestart () {
- //TODO auto-generated method stub
- Super. Onrestart ();
- LOG.D (Tag, "I ' m Onrestart");
- }
- @Override
- protected void OnPause () {
- //TODO auto-generated method stub
- Super. OnPause ();
- LOG.D (Tag, "I ' m onPause");
- }
- }
Android Activity life Cycle