Scenario: Open the application boot page A, then jump to home page B and close the boot page A. After the application is returned to the desktop, click the desktop icon to start A- >B again. We want to be able to go back to the desktop and click the icon to directly open the home page B;
Note: This scenario applies only to applications packaged with signing certificates. Debug installs applications without problems
In order to do this, we need to determine whether boot page A is started for the first time and whether it is the first Activity on the stack in the onCreate lifecycle method of boot page A, and if so, finish. This can be determined by the Activity’s isTaskRoot() method
IsTaskRoot () system source:
/**
* Return whether this activity is the root of a task. The root is the
* first activity in a task.
*
* @return True if this is the root activity, else false.
*/
@Override
public boolean isTaskRoot(a) {
try {
return ActivityTaskManager.getService().getTaskForActivity(mToken, true) > =0;
} catch (RemoteException e) {
return false; }}Copy the code
- Close the boot page if it is not the first activity on the stack
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("SplashActivity"."isTaskRoot :" + isTaskRoot());
if(! isTaskRoot()) {// If it is not the first activity in the stack, it is not started repeatedly
finish();
return;
}
setContentView(R.layout.activity_splash);
}
Copy the code
- Home click back to return to the desktop without destroying the page
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Click to return to the desktop
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent home = new Intent(Intent.ACTION_MAIN);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
home.addCategory(Intent.CATEGORY_HOME);
startActivity(home);
return true;
}
return super.onKeyDown(keyCode, event);
}
Copy the code
Making portal