How to remove a specific activity from the stack? here is an example, there is activty A, B, C, the user goes A-> B-> C, and activty C under certain conditions,

  1. I have to go back (activty B) (finish)
  2. it is necessary to go back (immediately to activty A bypassing activty B, without re-creating activty A, how to do it?)

// do this, it re-creates activty A

Intent intent = new Intent(this, A); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); 
  • Just do not keep it on the stack B, onBackPressed will translate immediately to A, well, or finish () depending on what is there - Shwarz Andrei

2 answers 2

To fence the code with the launch of onActivityResult() via onActivityResult() is irrational. You must assign Activate A android:launchMode="singleTask" . And by launching startActivity(new Intent(this, A) activation A will be pulled out of the stack without being recreated, closing the upper activations simultaneously.

    Answer to the questions:

    1. To return from Activation C to B, you do everything right, just call finish () on Activation C.

    2. To return from C to A, you need to add some logic. When you activate Activation C, do it through:

      startActivtyForResult (new Intent (this, C.class), 1234)

    When completing C, before calling finish (), in the reverse intent throw the flag, does it need to complete activation B or not:

      Intent answerIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putBoolean("need_finish", true); answerIntent.putExtras(bundle); setResult(RESULT_OK, answerIntent); 

    Activiti B processes the onActivityResult method. If the flag is true, then you also call finish () on:

     onActivityResult(int requestCode, int resultCode, Intent data){ if (requestCode == 1234 && data != null) { if (data.getBoolean("need_finish", true)) { finish(); } } }