Zach
Zach

Reputation: 10129

Remove an activity from stack in android

I want to remove an activity from stack using code.Heres my case

  1. From page A I am going to page B.
  2. From page B i have to return to page A using return button.
  3. In page B I am having a button which takes to page C.
  4. When I click that button in page B , I am calling
finish(); //to remove PageB from stack

Ok, Here is the issue, From Page C when I click the return button I am taken to page A. because it is in stack.

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

Please note that I cant call a finish() in page A when calling Page B because I want to return back to page A. Only case I dont want to return is when the button in page B is clicked.

How can I do this in android? Thanks

Upvotes: 4

Views: 9801

Answers (4)

Laurent'
Laurent'

Reputation: 2631

Surely there is a better answer on this page but, as a workaround, you could use SharedPreferences to pass a message to Activity A, requesting that it also finishes.

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();
    }
}

Activity 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();
}

Note that this is somewhat risky since the preferences survive the reboots and it can prevent A to start if, for example, your application is killed before A resumes. To work around this, you should also remove the message in A.onCreate()

Upvotes: 0

Ben Von Handorf
Ben Von Handorf

Reputation: 2336

Rather than calling startActivity in A when you start B, call startActivityForResult. Then in, your activity for A, handle onActivityResult.

Now, in B, when you open C, call setResult before calling finish. This will allow you to set some data to get passed back to A's onActivityResult method. Pass a flag to indicate that A should close itself and then call finish. Handle that flag in A's onActivityResult.

This way, each activity is responsible for closing itself and you're not artificially messing with the back stack. Using intent flags work fine in a simple A,B,C case, but will probably fall apart if these 3 screens are part of a larger solution (i.e. A,B and C are deep under a stack of activities you don't want to mess with).

Upvotes: 5

Arun Badole
Arun Badole

Reputation: 11097

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

Upvotes: 0

thaussma
thaussma

Reputation: 9886

Instead of finishing the current Activity you can jump directly to another Activity by starting an Intent.

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

Upvotes: 4

Related Questions