博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,
点击跳转到网站
Activity的android:launchMode属性:
Activity的管理模式:
standard:Activity是由任务栈管理的,每启动一个Activity,就会被放入栈中,按返回键,就会从栈顶移除一个Activity。
具体详解:
一、singleTop模式下Activity的生命周期如下
MainActivity设置为SingleTop 栈顶复用模式
<activity
android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
MainActivity中代码如下
public class MainActivity extends AppCompatActivity {
private Button btn_main;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, this.toString());
btn_main = findViewById(R.id.btn_main);
Log.i(TAG, "onCreate: ");
btn_main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i(TAG, "onNewIntent: ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart: ");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
}
首次进入
当点击按钮后,还是跳转到MainActivity,此时MainActivity在栈顶,就复用栈顶的Activity,生命周期如下
退出MainActivity后,生命周期如下
一、singleTask模式下Activity的生命周期如下
MainActivity跳转到SecondActivity,SecondActivity跳转到MainActivity,此时MainActivity在栈中已经存在,则可以复用。
SecondActivity代码如下
public class SecondActivity extends AppCompatActivity {
private Button btn;
private static final String TAG = "SecondActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.i(TAG, this.toString());
btn = findViewById(R.id.btn);
Log.i(TAG, "onCreate: ");
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i(TAG, "onNewIntent: ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart: ");
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
}
MainActivity跳转到SecondActivity两个Activity的生命周期如下
点击SecondActivity页面中button按钮跳转到MainActivity两个Activity的生命周期如下
在SecondActivity中启动MainActivity时,发现返回栈中已经存在MainActivity的实例,并且是在SecondActivity的下面,于是SecondActivity从返回栈中出栈,MainActivity重新成为了栈顶活动,于是SecondActivity中会执行onDestory()方法。并且现在返回栈中只剩下一个MainActivity实例了。