新建两个简单的activity?
ActivityFirst
public class ActivityFirst extends AppCompatActivity {
ActivityFirstBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
ActivityUtil.printLog("ActivityFirst onCreate");
super.onCreate(savedInstanceState);
binding = ActivityFirstBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.second.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(ActivityFirst.this, ActivitySecond.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
ActivityUtil.printLog("ActivityFirst onStart");
super.onStart();
}
@Override
protected void onStop() {
ActivityUtil.printLog("ActivityFirst onStop");
super.onStop();
}
@Override
protected void onDestroy() {
ActivityUtil.printLog("ActivityFirst onDestroy");
super.onDestroy();
}
@Override
protected void onPause() {
ActivityUtil.printLog("ActivityFirst onPause");
super.onPause();
}
@Override
protected void onNewIntent(Intent intent) {
ActivityUtil.printLog("ActivityFirst onNewIntent");
super.onNewIntent(intent);
}
@Override
protected void onResume() {
ActivityUtil.printLog("ActivityFirst onResume");
super.onResume();
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
ActivityUtil.printLog("ActivityFirst onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
ActivityUtil.printLog("ActivityFirst onSaveInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
}
ActivitySecond
public class ActivitySecond extends AppCompatActivity {
ActivitySecondBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
ActivityUtil.printLog("ActivitySecond onCreate");
super.onCreate(savedInstanceState);
binding = ActivitySecondBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.first.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(ActivitySecond.this, ActivityFirst.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
ActivityUtil.printLog("ActivitySecond onStart");
super.onStart();
}
@Override
protected void onStop() {
ActivityUtil.printLog("ActivitySecond onStop");
super.onStop();
}
@Override
protected void onDestroy() {
ActivityUtil.printLog("ActivitySecond onDestroy");
super.onDestroy();
}
@Override
protected void onPause() {
ActivityUtil.printLog("ActivitySecond onPause");
super.onPause();
}
@Override
protected void onNewIntent(Intent intent) {
ActivityUtil.printLog("ActivitySecond onNewIntent");
super.onNewIntent(intent);
}
@Override
protected void onResume() {
ActivityUtil.printLog("ActivitySecond onResume");
super.onResume();
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
ActivityUtil.printLog("ActivitySecond onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
ActivityUtil.printLog("ActivitySecond onSaveInstanceState");
super.onRestoreInstanceState(savedInstanceState);
}
}
First启动Second后的执行顺序
ActivityFirst onPause
ActivitySecond onCreate
ActivitySecond onStart
ActivitySecond onResume
ActivityFirst onStop
ActivityFirst onSaveInstanceState
Second返回First的顺序
Second旋转的执行顺序
Second旋转后返回First的执行顺序
Second设置为singleTask
android:launchMode="singleTask"
Second->First->Second
静态设置 | 动态设置 | singleTask | FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP | singleTop | FLAG_ACTIVITY_SINGLE_TOP |
|