Android 关于Activity生命周期
程序员文章站
2022-06-23 16:39:56
先看一张图下面会用程序进行演示:package com.example.myapplication;import androidx.annotation.NonNull;import androidx.appcompat.app.AppCompatActivity;import androidx.core.view.KeyEventDispatcher;import android.content.ComponentName;import android.content.Inte...
先看一张图
下面会用程序进行演示:
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.KeyEventDispatcher;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity { //继承
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //父类之中调用
setContentView(R.layout.activity_main);//R(资源调用的一个类)选择了一个界面显示了出来
/* Log.i("MainActivityMSG","iiMessage");
Log.d("MainActivityMSG","diMessage");
Log.e("MainActivityMSG","eiMessage");
Log.w("MainActivityMSG","wiMessage"); //可以进行日志的输出,是调试的重要手段,会在日志中打印出后面这句话
*/
Log.e("activityState","MainActivity_onCreate");
}
protected void onStart(){
super.onStart();
Log.e("activityState","MainActivity_onStart");
}
protected void onRestart(){
super.onRestart();
Log.e("activityState","MainActivity_onRestart");
}
protected void onResume() {
super.onResume();
Log.e("activityState", "MainActivity_onResume");
}
protected void onPause(){
super.onPause();
Log.e("activityState","MainActivity_onPause");
}
protected void onStop(){
super.onStop();
Log.e("activityState","MainActivity_onStop");
}
protected void onDestroy(){
super.onDestroy();
Log.e("activityState","MainActivity_onDestroy");
}
public void sendMessage(View view){
//相应按钮的事件
EditText msg=findViewById(R.id.message);
String s=msg.getText().toString();
Log.e("sendMessage",s);
//写法一 Intent intent =new Intent(this ,SecondActivity.class);
// this.startActivity(intent);
//写法二
//Intent intent=new Intent();
//intent.setClassName(this,"com.example.myapplication.SecondActivity");
//startActivity(intent);
//写法三
//Intent intent =new Intent();
//ComponentName cname=new ComponentName(this ,SecondActivity.class);
//intent.setComponent(cname);
//startActivity(intent);
//写法四 隐式启动
// Intent intent=new Intent("android.intent.action.nextActivity");
// startActivity(intent);
}
}
下面进行第一次启动:可以看到启动后如下日志
020-12-04 09:30:46.185 28359-28359/com.example.myapplication E/activityState: MainActivity_onCreate
2020-12-04 09:30:46.189 28359-28359/com.example.myapplication E/activityState: MainActivity_onStart
2020-12-04 09:30:46.195 28359-28359/com.example.myapplication E/activityState: MainActivity_onResume
2020-12-04 09:30:46.320 28359-28391/com.example.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2020-12-04 09:30:46.320 28359-28391/com.example.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2020-12-04 09:30:46.320 28359-28391/com.example.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2020-12-04 09:30:46.320 28359-28391/com.example.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2020-12-04 09:31:11.620 28359-28359/com.example.myapplication E/activityState: MainActivity_onPause
2020-12-04 09:31:11.686 28359-28359/com.example.myapplication E/activityState: MainActivity_onStop
2020-12-04 09:32:31.253 28359-28359/com.example.myapplication E/activityState: MainActivity_onRestart
2020-12-04 09:32:31.272 28359-28359/com.example.myapplication E/activityState: MainActivity_onStart
2020-12-04 09:32:31.329 28359-28359/com.example.myapplication E/activityState: MainActivity_onResume
2020-12-04 09:32:33.593 28359-28359/com.example.myapplication E/activityState: MainActivity_onPause
2020-12-04 09:32:33.763 28359-28359/com.example.myapplication E/activityState: MainActivity_onStop
可以验证到上面的流程图是正确的。最后再来一张总结图
后面做了一下类似的实验,只不过这次有两个Activity主要观察他的跳转的时候的关系。这里就不记录具体的做法了。下面是打印出的日志。
2020-12-04 10:14:13.636 10218-10218/com.example.myapplication E/activityState: MainActivity_onCreate
2020-12-04 10:14:13.643 10218-10218/com.example.myapplication E/activityState: MainActivity_onStart
2020-12-04 10:14:13.656 10218-10218/com.example.myapplication E/activityState: MainActivity_onResume
2020-12-04 10:14:13.874 10218-10273/com.example.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2020-12-04 10:14:13.876 10218-10273/com.example.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2020-12-04 10:14:13.876 10218-10273/com.example.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2020-12-04 10:14:13.877 10218-10273/com.example.myapplication E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2020-12-04 10:14:54.683 10218-10218/com.example.myapplication E/activityState: MainActivity_onPause
2020-12-04 10:14:54.733 10218-10218/com.example.myapplication E/activityState: MainActivity_onStop
2020-12-04 10:14:58.273 10218-10218/com.example.myapplication E/activityState: MainActivity_onRestart
2020-12-04 10:14:58.286 10218-10218/com.example.myapplication E/activityState: MainActivity_onStart
2020-12-04 10:14:58.329 10218-10218/com.example.myapplication E/activityState: MainActivity_onResume
2020-12-04 10:15:03.808 10218-10218/com.example.myapplication E/activityState: MainActivity_onPause
2020-12-04 10:15:04.085 10218-10218/com.example.myapplication E/activityState: SecondActivity_onCreate
2020-12-04 10:15:04.087 10218-10218/com.example.myapplication E/activityState: SecondActivity_onStart
2020-12-04 10:15:04.091 10218-10218/com.example.myapplication E/activityState: SecondActivity_onResume
2020-12-04 10:15:04.805 10218-10218/com.example.myapplication E/activityState: MainActivity_onStop
2020-12-04 10:15:08.508 10218-10218/com.example.myapplication E/activityState: SecondActivity_finish关闭按钮点击时打出的log
2020-12-04 10:15:08.529 10218-10218/com.example.myapplication E/activityState: SecondActivity_onPause
2020-12-04 10:15:08.576 10218-10218/com.example.myapplication E/activityState: MainActivity_onRestart
2020-12-04 10:15:08.577 10218-10218/com.example.myapplication E/activityState: MainActivity_onStart
2020-12-04 10:15:08.578 10218-10218/com.example.myapplication E/activityState: MainActivity_onResume
2020-12-04 10:15:09.144 10218-10218/com.example.myapplication E/activityState: SecondActivity_onStop
2020-12-04 10:15:09.148 10218-10218/com.example.myapplication E/activityState: SecondActivity_onDestroy
2020-12-04 10:16:08.492 10218-10218/com.example.myapplication E/activityState: MainActivity_onPause
2020-12-04 10:16:08.532 10218-10218/com.example.myapplication E/activityState: MainActivity_onStop
本文地址:https://blog.csdn.net/a15929748502/article/details/110559090
上一篇: 福建旅游十大必去景点
推荐阅读
-
Android 入门第十讲02-广播(广播概述,使用方法(系统广播,自定义广播,两个activity之间的交互和传值),EventBus使用方法,数据传递,线程切换,Android的系统广播大全)
-
Android中Activity之间跳转和参数传递的实例
-
Android中两个Activity之间数据传递及返回问题
-
Android中关于自定义相机预览界面拉伸问题
-
Android编程之四种Activity加载模式分析
-
Android实现从activity中停止Service的方法
-
Android仿微信activity滑动关闭效果
-
Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法
-
Android Parcelable序列化自定义类集合在Activity间传递
-
android显示意图激活另一个Activity的方法