Android-活动的生命周期详解
- Activity https://developer.android.google.cn/training/basics/activity-lifecycle/
-
管理 Activity 生命周期 https://developer.android.google.cn/training/basics/activity-lifecycle/
-
Activity生命周期:
本次演示的界面:
MainActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<Button
android:id="@+id/NormalActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start NormalActivity"
android:textAllCaps="false"
android:textSize="30sp"
/>
<Button
android:id="@+id/DialogActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Start DialogActivity"
android:textAllCaps="false"
android:textSize="30sp"
/>
</LinearLayout>
package com.zgh.leo.activitylifecycle;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: ");
super.onCreate(savedInstanceState);
if (savedInstanceState!=null){
//获取未保存的数据
savedInstanceState.getString("data");
}
setContentView(R.layout.activity_main);
//初始化Button及设置监听
init();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//保存未保存的数据
outState.putString("data","未保存的数据");
}
private void init() {
button1 =findViewById(R.id.NormalActivity);
button2 =findViewById(R.id.DialogActivity);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,NormalActivity.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(MainActivity.this,DialogActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onRestart() {
Log.d(TAG, "onRestart: ");
super.onRestart();
}
@Override
protected void onDestroy() {
Log.d(TAG, "onDestroy: ");
super.onDestroy();
}
@Override
protected void onStop() {
Log.d(TAG, "onStop: ");
super.onStop();
}
@Override
protected void onPause() {
Log.d(TAG, "onPause: ");
super.onPause();
}
@Override
protected void onResume() {
Log.d(TAG, "onResume: ");
super.onResume();
}
@Override
protected void onStart() {
Log.d(TAG, "onStart: ");
super.onStart();
}
}
NormalActivity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NormalActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="NormalActivity"
android:textSize="30sp"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.zgh.leo.activitylifecycle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class NormalActivity extends AppCompatActivity {
private static final String TAG = "NormalActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_normal);
}
}
DialogActivity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DialogActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="DialogActivity"
android:textSize="30sp"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.zgh.leo.activitylifecycle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class DialogActivity extends AppCompatActivity {
private static final String TAG = "DialogActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
}
1.当启动Mainactivity时,会依次调用:
2019-01-18 22:24:54.676 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onCreate:
2019-01-18 22:24:54.928 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onStart:
2019-01-18 22:24:54.930 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onResume:
2.当点击StartNormalActivity按钮时,会依次调用:
2019-01-18 22:26:16.492 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onPause:
2019-01-18 22:26:17.319 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onStop:
3.点击返回键,依次调用:
2019-01-18 22:27:35.260 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity:onRestart:
2019-01-18 22:27:35.261 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onStart:
2019-01-18 22:27:35.261 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onResume:
4.当点击StartDialogActivity按钮时,会依次调用:
2019-01-18 22:29:07.696 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onPause:
5.点击返回键,会依次调用:
2019-01-18 22:29:51.164 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onResume:
6.继续点击返回键退出应用时,会依次调用:
2019-01-18 22:30:51.865 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onPause:
2019-01-18 22:30:52.568 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity: onStop:
2019-01-18 22:30:52.571 23062-23062/com.zgh.leo.activitylifecycle D/MainActivity:onDestroy:
Android生命周期演示代码:https://download.csdn.net/download/qq_41113081/10926794