android中Activity生命周期的详细过程分析

android中Activity生命周期的详细过程分析

Activity是安卓中相当重要的一个组件,理解它有利于我们后期更好的进行相关的开发,接下来我将演示一个demo来详细讲述Activity的生命周期过程。
首先写两个布局文件ks.xml和ks1.xml,用来实现两个界面的跳转,上面写上简单的button,代码如下:
ks.xml:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@android:color/holo_red_light"
    android:gravity="center_vertical">
    <ImageView
        android:id="@+id/search"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:src="@drawable/title_city" />
    <ImageView
        android:id="@+id/division"
        android:layout_width="1dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@+id/search"
        android:background="@android:color/white"/>

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转"/>

</RelativeLayout>

ks1.xml:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/afterlogin"
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:layout_marginBottom="30dp"
    android:layout_marginTop="50dp"
    android:textSize="30dp"
    android:textColor="#000000"/>
<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="返回"/>
除了布局文件,当然还有两个Activity,用于控制两个界面的跳转,Activity的几个重要方法也在这里面体现。

Activity1:
public class Activity1 extends Activity {
private static final String TAG = “ks”;
private Button btn;

@Override
protected void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    Log.i(TAG, "The activity1 state--------onCreate");
    setContentView(R.layout.ks);
    btn = (Button) findViewById(R.id.btn1);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Activity1.this, Activity2.class);
            startActivity(i);
        }
    });
}
protected void onStart() {
    super.onStart();
    Log.i(TAG, "The activity1 state--------onStart");

}
protected void onRestart() {
    super.onRestart();
    Log.i(TAG, "The activity1 state--------onRestart");
}
//重新开始
protected void onResume() {
    super.onResume();
    Log.i(TAG, "The activity1 state--------onResume");
}
protected void onPause() {
    super.onPause();
    Log.i(TAG, "The activity1 state--------onPause");
    if (isFinishing()) {
        Log.w(TAG, "The activity1 will be destroyed!");
    } else {
        Log.w(TAG, "The activity1 is just pausing!");
    }
}
protected void onStop() {
    super.onStop();
    Log.i(TAG, "The activity1 state--------onStop");
}
protected void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "The activity1 state--------onDestory");
}

}

Activity2的代码:
public class Activity2 extends Activity {
private Button btn2;
private static final String TAG = “ks”;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "The activity2 state--------onCreate");
    setContentView(R.layout.ks1);

    btn2 = (Button) findViewById(R.id.btn2);
    btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Activity2.this, Activity1.class);
            startActivity(i);
        }
    });
}
protected void onStart() {
    super.onStart();
    Log.i(TAG, "The activity2 state--------onStart");

}
protected void onRestart() {
    super.onRestart();
    Log.i(TAG, "The activity2 state--------onRestart");
}
//重新开始
protected void onResume() {
    super.onResume();
    Log.i(TAG, "The activity2 state--------onResume");
}
protected void onPause() {
    super.onPause();
    Log.i(TAG, "The activity2 state--------onPause");
    if (isFinishing()) {
        Log.w(TAG, "The activity2 will be destroyed!");
    } else {
        Log.w(TAG, "The activity2 is just pausing!");
    }
}
protected void onStop() {
    super.onStop();
    Log.i(TAG, "The activity2 state--------onStop");
}
protected void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "The activity2 state--------onDestory");
}

}
下面是执行过程的分析:
当启动activity1时候,activity1所对应的ks.xml界面打开
在这里插入图风风光片描述此时对应的各个方法的顺序是可见
可见启动界面后,依次执行onCreate,onStart,onResume,
点击跳转button,界面走到ks1.xml
在这里插入图片描述
此时的打印顺序为
在这里插入图片描述可见,当发生从activity1跳转到activity2时,activity1先暂停,即onPause,然后activity2依次创建,开始,执行,等到activity2界面出来之后,activity1stop。
我们再点返回button,重新回到第一个界面,此时的执行顺序为
在这里插入图片描述
可见,和之前类似,activity2先暂停,activity1再次被创建,进入activity1界面,activity2stop,如此反复循环。
我们发现,这里并没有出现activity被销毁的情况,即并没有打印出onDestroy的方法。
如果只是在两个button上相互跳转,两个activity只会stop,只有当按下模拟机中的back键时候,某一个activity会被销毁。

猜你喜欢

转载自blog.csdn.net/ning1994724/article/details/84110499