这里说了两个Activity之间,用默认模式切换的生命周期。
代码:
- MainActivity
package com.example.lifecycletest;
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 {
Button a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a = findViewById(R.id.btn);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.w("tagchh","点击了按钮开始跳转");
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
Log.w("tagchh","MainActivity onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.w("tagchh","MainActivity onStart");
}
@Override
protected void onStop() {
super.onStop();
Log.w("tagchh","MainActivity onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.w("tagchh","MainActivity onDestory");
}
@Override
protected void onPause() {
super.onPause();
Log.w("tagchh","MainActivity onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.w("tagchh","MainActivity onResume");
}
@Override
protected void onRestart() {
super.onRestart();
Log.w("tagchh","MainActivity onRestart");
}
}
- SecondAcitvity
package com.example.lifecycletest;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
/**
* @author by Zero_Hwa,
* @blog https://blog.youkuaiyun.com/Zero_HWA
* @date on 2019/10/4.
* Do your best in the process and have a clear conscience in the end
* PS: Not easy to write code, please indicate.
*/
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Log.w("tagchh","SecondActivity onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.w("tagchh","SecondActivity onStart");
}
@Override
protected void onStop() {
super.onStop();
Log.w("tagchh","SecondActivity onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.w("tagchh","SecondActivity onDestory");
}
@Override
protected void onPause() {
super.onPause();
Log.w("tagchh","SecondActivity onPause");
}
@Override
protected void onResume() {
super.onResume();
Log.w("tagchh","SecondActivity onResume");
}
@Override
protected void onRestart() {
super.onRestart();
Log.w("tagchh","SecondActivity onRestart");
}
@Override
public void onBackPressed() {
super.onBackPressed();
Log.w("tagchh","SecondActivity点击了返回键");
}
}
- MainAcitvity的layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="goto_another_activity"
android:id="@+id/btn"/>
</LinearLayout>
弹出界面的流程是MainActivity-点击Button-跳转到SecondActivity-点击返回-跳回MainActivity。
得到结果:
结果分析:
1.首先是MainActivity的onCreate,onStart,onResume,然后MainActivity显示出来。
2.接着点击按钮开始跳转,MainActivity的onPause之后,SecondActivity开始onCreate,onStart,onResume.
3. 第二个Activity显示出来之后,第一个Activity才onStop。
4. SecondActivity点击返回键【即back】,然后开始SecondActivity的onPause,等MainActivity的onRestart,onStart,onResume生命周期执行完之后,及MainActivity执行完之后,SecondActivity的onStop和onDestory才开始执行。
小结
默认启动模式下,Activity之间的跳转是当前的Activity先到onPause生命周期,然后等跳转目的Activity加载完,显示出来【即onResume】之后,才开始执行onStop之后的程序。
ps:其他模式还没试验不过小结这点是没错的。