Android back stack modification

I would like to change the rear stack in my Android application as such:

Right now, here is the thread:

A β†’ B β†’ C β†’ D β†’ E β†’ F

I want to be able to change the back stack, so when the user goes to action F, D and E are erased from the stack. therefore, the stream F β†’ C if the user falls back.

In addition, from F the user can go to activity B, this should also remove C, D, E and F.

I saw some information about the ability to clear the stack or delete the top element, but I want to remove several elements from the stack when the action is launched.

Any help is appreciated, thanks very much.

+6
source share
2 answers

You can create an intent with the intent.FLAG_ACTIVITY_CLEAR_TOP flag from F to C. Then you will need to call startActivity () with the intent and call this to happen onBackPressed or something like that.

 Intent i = new Intent(this, C.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(i) 

See this answer, which also says that C will not restart when you return to it: fooobar.com/questions/542981 / ...

What FLAG_ACTIVITY_CLEAR_TOP will do is return to the most recent instance of C activity on the stack and then clear everything above it. However, this can lead to the re-creation of activity. If you want to make sure that it will be the same activity instance, use FLAG_ACTIVITY_SINGLE_TOP . From the documentation:

The currently executable instance of action B in the above example will either get a new intention, which you start here in your onNewIntent (), or it will be completed and restarted with a new intention. If he declared his startup mode "multiple" (default), and you did not set FLAG_ACTIVITY_SINGLE_TOP in the same intention, then it will be completed and recreated; for all other starts, or if FLAG_ACTIVITY_SINGLE_TOP is set, this intent will be delivered to the current instance of onNewIntent ().

Edit: Here is a sample code similar to what you want to do:

 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { Intent a = new Intent(this, C.class); a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(a); return true; } return super.onKeyDown(keyCode, event); } 

Source code example: fooobar.com/questions/89680 / ...

+9
source

You will need android:excludeFromRecents="true" , which should be added to the activity tag in the manifest file. It stops all actions to go to the stack that has this tag in the Activity tag in the manifest.

Code example

  <activity android:name="com.xx.xx.ActivityName" android:excludeFromRecents="true" </activity> 

and make sure that you call the finish() method (after starting another action) in those actions that you do not want to be on the action stack.

+2
source

Source: https://habr.com/ru/post/919879/


All Articles