Activity启动方式有四种,分别是:
<activity android:name=".MainActivity">
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, ">>> onCreate: " + getTaskId());
button = findViewById(R.id.main_stand);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, ">>> onDestroy: " + getTaskId());
}
}
在原来的Activity上点击新建,无论是否相同都会创建实例
<activity
android:name=".MainActivity2"
android:launchMode="singleTop" />
<activity android:name=".MainActivity">
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button button;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, ">>> onCreate: " + getTaskId());
button = findViewById(R.id.main_stand);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
});
button2 = findViewById(R.id.main_singleTop);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, ">>> onDestroy: " + getTaskId());
}
}
当相同的Activity位于栈顶,不会新建,不位于栈顶时则新建
<activity
android:name=".MainActivity2"
android:launchMode="singleTask" />
<activity android:name=".MainActivity">
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button button;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, ">>> onCreate: " + getTaskId());
button = findViewById(R.id.main_stand);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
});
button2 = findViewById(R.id.main_singleTask);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, ">>> onDestroy: " + getTaskId());
}
}
当实例不存在时创建;当存在且在栈顶时,复用;当存在且不在栈顶时,销毁之上的Activity,让目标位置栈顶
<activity
android:name=".MainActivity2"
android:launchMode="singleInstance" />
<activity android:name=".MainActivity">
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button button;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, ">>> onCreate: " + getTaskId());
button = findViewById(R.id.main_stand);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity.class));
}
});
button2 = findViewById(R.id.main_singleInstance);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MainActivity2.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, ">>> onDestroy: " + getTaskId());
}
}
退栈时,首先在当前Activity所在的Task栈进行退栈操作,而不是按照进栈的顺序进行退栈,即先把当前Task出栈完成,再出栈其他Task.
作者:氷泠
链接:https://www.imooc.com/article/302444
来源:慕课网
Reference
https://www.imooc.com/article/302444
https://www.cnblogs.com/foxy/p/7660278.html
https://www.cnblogs.com/androidWuYou/p/5887807.html