Android Studio 之 Activity 的生命周期
Posted 专注WEB开发技术 @ 修身、齐家、治国、平天下!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 之 Activity 的生命周期相关的知识,希望对你有一定的参考价值。
翻转屏幕,会重新加载Activity
package com.example.activitylivecycle; import android.os.Bundle; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { //定义 TAG 方便自己容易查找日志 //翻转屏幕,Activity 会重新创建加载 String TAG = "myTag"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG,"onCreate: "); } @Override protected void onStart() { super.onStart(); Log.d(TAG,"onStart: "); } @Override protected void onResume() { super.onResume(); Log.d(TAG,"onResume: "); } @Override protected void onPause() { super.onPause(); Log.d(TAG,"onPause: "); } @Override protected void onStop() { super.onStop(); Log.d(TAG,"onStop: "); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG,"onRestart: "); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy: "); } }
打开app后产生的事件
2019-11-01 19:28:59.070 3334-3334/com.example.activitylivecycle D/myTag: onCreate:
2019-11-01 19:28:59.071 3334-3334/com.example.activitylivecycle D/myTag: onStart:
2019-11-01 19:28:59.072 3334-3334/com.example.activitylivecycle D/myTag: onResume:
屏幕翻转后执行顺序
2019-11-01 19:29:10.748 3334-3334/com.example.activitylivecycle D/myTag: onPause:
2019-11-01 19:29:10.772 3334-3334/com.example.activitylivecycle D/myTag: onStop:
2019-11-01 19:29:10.772 3334-3334/com.example.activitylivecycle D/myTag: onDestroy:
2019-11-01 19:29:10.792 3334-3334/com.example.activitylivecycle D/myTag: onCreate:
2019-11-01 19:29:10.807 3334-3334/com.example.activitylivecycle D/myTag: onStart:
2019-11-01 19:29:10.811 3334-3334/com.example.activitylivecycle D/myTag: onResume:
以上是关于Android Studio 之 Activity 的生命周期的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio探索之路系列之六:Android Studio加入依赖