Android 关于Activity生命周期
程序员文章站
2022-03-12 21:50:08
先看一张图下面会用程序进行演示: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
上一篇: javascript动态添加元素
下一篇: React — 从表单认识的受控组件
推荐阅读
-
Android关于WebView中无法定位的问题解决
-
故事讲解Activity生命周期(猫的一生)
-
关于ADB的Android Debug Bridge(安卓调试桥)那些事
-
Android 中启动自己另一个程序的activity如何实现
-
Android 启动另一个App/apk中的Activity实现代码
-
android弹出activity设置大小的方法
-
android游戏载入的activity跳转到游戏主菜单的activity具体实现
-
Android关于SeekBar无法点击到最大值问题解决方法记录(推荐)
-
Android关于获取时间的记录(小结)
-
Activity生命周期与启动模式图文解说