写一个demo,实现两个页面之间的跳转。重写7个生命周期方法,在方法中打印日志,观察状态的变化。
MainActivity
设置一个常量。
private static final String TAG = "hello_activity_1";
重写7个生命周期。
在生命周期方法中,如果设备调用该方法,则使用日志的方式打印出来。
Log.d(TAG, "Activity_1 onCreate");
MainActivity_1.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
/**
* 继承AppCompatActivity类,
* 需要在AndroidManifest.xml文件中
* 指定 android:theme="@style/Theme.AppCompat.xxx"
*/
public class MainActivity extends AppCompatActivity {
private static final String TAG = "hello_activity_1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Activity_1 onCreate");
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, MainActivity_2.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "Activity_1 onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "Activity_1 onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "Activity_1 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "Activity_1 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "Activity_1 onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "Activity_1 onRestart");
}
}
MainActivity_2.java
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity_2 extends AppCompatActivity {
private static final String TAG = "hello_activity_2";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
Log.d(TAG, "Activity_2 onCreate");
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity_2.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "Activity_2 onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "Activity_2 onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "Activity_2 onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "Activity_2 onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "Activity_2 onDestroy");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "Activity_2 onRestart");
}
}
启动
控制台信息
---------------------------- PROCESS STARTED (14765) for package com.example.myapplication ----------------------------
2023-08-04 14:50:43.868 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onCreate
2023-08-04 14:50:44.013 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStart
2023-08-04 14:50:44.016 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onResume
onCreate()
系统在首次创建Activity时会调用,进入“已创建”的状态。
在onCraete()方法中通过setContentView()方法指定布局文件。
setContentView(R.layout.activity_main);
onStart()
系统调用onStart()方法,进入“已开始”状态。
应用通过此方法来初始化维护界面的代码。
onResume()
Activity不会一直处于“已开始”状态。一旦此回调结束,Activity 便会进入“已恢复”状态,系统将调用onResume()方法。
这是应用与用户互动的状态。应用会一直保持这种状态,直到某些事件发生,让焦点远离应用。此类事件包括接到来电、用户导航到另一个 Activity,或设备屏幕关闭。
点击跳转按钮
控制台信息
---------------------------- PROCESS STARTED (14765) for package com.example.myapplication ----------------------------
2023-08-04 14:50:43.868 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onCreate
2023-08-04 14:50:44.013 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStart
2023-08-04 14:50:44.016 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onResume
2023-08-04 14:51:00.845 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onPause
2023-08-04 14:51:00.885 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onCreate
2023-08-04 14:51:00.887 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onStart
2023-08-04 14:51:00.890 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onResume
2023-08-04 14:51:01.561 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStop
分析
activity_1:
从日志信息中可以看到,activity_1调用了**onPause()**方法,进入了“已暂停”状态。
activity_2:
activity_2调用了**onCreate() onStart() onResume()**方法。
activity_1:
在activity_2调用结束后,activity_1调用了**onStop()**方法。
onPause()
“已暂停”状态,此时activity_1还没有被销毁,只是不再位于前台(可能在后台,资源并没有完全释放?)。
activity_1会保持这个状态,直到其恢复或者变成对用户完全不可见。
- 若恢复,系统将再次调用onResume(),之后,系统会让Activity实例继续驻留在内存中。
- 若变成完全不可见,系统会调用onStop()。
onStop()
在activity_2调用结束后,activity_1调用了**onStop()**方法。在生命周期图中onStop()已经是倒数第二步,应用应释放或调整在应用对用户不可见的无用资源。
进入“已停止”状态后,Activity对象会继续驻留在内存中,Activity的后续活动:
- 返回与用户互动,系统将调用onRestart()。
- 结束运行并消失,系统将调用onDestroy()。
跳转回第一个页面
控制台信息
---------------------------- PROCESS STARTED (14765) for package com.example.myapplication ----------------------------
2023-08-04 14:50:43.868 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onCreate
2023-08-04 14:50:44.013 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStart
2023-08-04 14:50:44.016 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onResume
2023-08-04 14:51:00.845 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onPause
2023-08-04 14:51:00.885 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onCreate
2023-08-04 14:51:00.887 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onStart
2023-08-04 14:51:00.890 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onResume
2023-08-04 14:51:01.561 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStop
2023-08-04 15:21:56.684 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onPause
2023-08-04 15:21:56.705 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onCreate
2023-08-04 15:21:56.749 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStart
2023-08-04 15:21:56.752 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onResume
2023-08-04 15:21:57.367 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onStop
在任务栏上划应用
控制台信息
---------------------------- PROCESS STARTED (14765) for package com.example.myapplication ----------------------------
2023-08-04 14:50:43.868 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onCreate
2023-08-04 14:50:44.013 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStart
2023-08-04 14:50:44.016 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onResume
2023-08-04 14:51:00.845 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onPause
2023-08-04 14:51:00.885 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onCreate
2023-08-04 14:51:00.887 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onStart
2023-08-04 14:51:00.890 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onResume
2023-08-04 14:51:01.561 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStop
2023-08-04 15:21:56.684 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onPause
2023-08-04 15:21:56.705 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onCreate
2023-08-04 15:21:56.749 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStart
2023-08-04 15:21:56.752 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onResume
2023-08-04 15:21:57.367 14765-14765 hello_activity_2 com.example.myapplication D Activity_2 onStop
2023-08-04 15:22:51.099 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onPause
2023-08-04 15:22:51.134 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onStop
2023-08-04 15:23:39.091 14765-14765 hello_activity_1 com.example.myapplication D Activity_1 onDestroy
分析
我将缩小进入任务栏和上划释放两个步骤间隔了一段时间,可以看出,在最终的销毁之前,activity_1和activity_2都会调用onStop()进入停止状态。
onDestroy()
“销毁”状态,释放所有的资源。系统调用此回调的原因如下:
- Activity即将结束,用户彻底的关闭了Activity或者系统为Activity调用了finish()。
- 由于配置变更,例如:设备旋转、多窗口模式,系统暂时销毁activity。
参考文档
https://developer.android.google.cn/guide/components/activities/activity-lifecycle?hl=zh-cn