Search code examples
androidandroid-activityactivity-stack

popping a known number of activities from stack


in the process of building an app, I have an Activity lets call it A, and i open in multiple times, as A(1) -> A(2) -> A(3) -> ... -> A(m) -> ... -> A(n).

In the app I want to pop all the way back to A(m), is there a way to pop activities all the way back to a chosen one?

i already saw a few answers that had the FLAG_ACTIVITY_CLEAR_TOP but it seems to me like that is only to clear the whole stack and go to the first element in the stack and that is not what I'm trying to get.

update:

what I'm trying to achieve here is as follows: I have a list of questions, and the user can choose a different answer for each one.

once he chooses, the question and the answer go into a list of history, and another question is prompted. at any given point, the user can click a history button and go back to the question he chooses.

all the Q&A activities are the same, so going back to a question means popping the stack until getting there.


Solution

  • If I understand you correctly you are saying that you have N number of the SAME activity and wish to go back to some arbitrary starting point activity in the stack?

    I am not totally clear on whether you know ahead of time that all activities forward of your "sticky" activity should be finished or if this is determined somewhere along the way

    I don't know of any arbitrary way of popping N activities off the stack when they are duplicate activities but you could roll your own, such as (not finished code)

    If you know ahead of time that A(M) will be sticky before it launches the next activity(s) you could just tell each forward activity that it needs to kill itself once it launches it's new task by passing it a flag then when the last activity in the chain ends you are back to A(M)

    in A(...) 
    startSelf()
    {
    
       Intent I = new Intent(this,MyActivity.class);
       if (bFinishSelf || bFinishForward) 
         I.putExtra("finishself", 1);
    
       if (Finishelf)  
         finish();
    
    
       startActivity(I); 
    
    in ... all A(...) onCreate( ... 
    
    Bundle b = getIntent().getExtras(); 
    if(b !=null && extras.getInt("finishself");
      bFinishSelf = true; 
    

    Then when you start a new activity check if the finishself flag is set and if so call finish() after starting new activity ...

    If on the other hand A(z) is the one that determines you need to return to A(...) then I guess in A(z) you could tell the calling parent(s) that they need to kill themselves until reaching some ID contained in data ...

    A(M) startActivityForResult(...)

    A(Z) somehow you determine that A(M) should be the sticky activity ...

    finshAndStopAt(int ID) {
      Intent I = new Intent(); 
      I.putExtra("finish",ID); 
    
      if (getParent() == null) {
        setResult(Activity.RESULT_OK, I); 
      } else {
        getParent().setResult(Activity.RESULT_OK, I); 
      }
      finish();
    

    }

    All A(z ... m) (parent's) would monitor for the return data

    @Override void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        // If we are asked to finish ourselves, pass it on ...    
        if (null != data && data.getInt("finish",stopAt) != myID) {
           if (getParent() == null) {
              setResult(Activity.RESULT_OK, data); 
           } else {
              getParent().setResult(Activity.RESULT_OK, data); 
           finish();
        }
        // else we can hang around and are the sticky activity
    }
    

    It's a bit of pain and maybe someone knows of an easier method