Remove action from stack in android

I want to remove an action from the stack using code. This is my case

  • From page A, I will go to page B.
  • From page B, I need to return to page A using the back button.
  • On page B, I have a button that goes to page C.
  • When I press this button on page B, I call
finish(); //to remove PageB from stack 

So here is the problem: From Page C, when I press the back button, I got to page A. because it is on the stack.

I want to remove page A from the stack when I click the button on page B.

Please note that I cannot call the finish () on page A when invoking page B, because I want to return to page A. Only the case that I do not want to return is when the button on page B is pressed.

How can I do this in Android? Thanks

+4
source share
4 answers

Instead of calling startActivity in when starting B, call startActivityForResult . Then in your activity for A, handle onActivityResult .

Now, in B, when you open C, call setResult before setResult final. This will allow you to set some data to pass back to the A onActivityResult method. Skip the flag to indicate that A should close itself, and then call finish . Process this flag in onActivityResult .

Thus, each action is responsible for closing yourself, and you do not artificially mess with the back stack. Using intent flags works fine in the simple case of A, B, C, but will probably fall apart if these 3 screens become part of a larger solution (i.e. A, B and C are deep under the stack of actions that you don't want to mess with )

+5
source

Instead of completing the current Activity, you can go directly to another Office by running Intent.

 Intent intent = new Intent(this, MyTarget.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); 
+4
source

You can use startActivity () to call A again from B.

0
source

Of course, there is a better answer on this page , but as a workaround you can use SharedPreferences to send an Activity A message requesting that it also ends.

Activity A:

 public class A extends Activity { public static final String CLOSE_A_ON_RESUME = "CLOSE_A_ON_RESUME"; @Override public void onResume(){ super.onResume(); //Retrieve the message SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this); boolean IShouldClose=mPrefs.getBoolean(A.CLOSE_A_ON_RESUME,false); if (IShouldClose){ //remove the message (will always close here otherwise) mPrefs.edit().remove(A.CLOSE_A_ON_RESUME).commit(); //Terminate A finish(); } } 

Action C:

 public class C extends Activity { /* * Stores an application wide private message to request that A closes on resume * call this in your button click handler */ private void finishCthenA(){ //Store the message SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this); mPrefs.edit().putBoolean(A.CLOSE_A_ON_RESUME,true).commit(); //finish C finish(); } 

Please note that this is somewhat risky, since preferences survive reboots, and this can prevent A from starting if, for example, your application is killed before resuming. To get around this, you should also delete the message in A.onCreate ()

0
source

All Articles