Logic
Logic

Reputation: 2258

remove previous "n" activities from back stack

I have navigation like A -> B -> C -> D -> E -> F

In F if I select an item, I need to navigate to C.

from C if I back press I need to go back like C -> B -> A -> Exit app

How to do this ?

How to remove previous n activities from back stack ??

Upvotes: 1

Views: 455

Answers (4)

Shahar Gluzman
Shahar Gluzman

Reputation: 71

I suggest the following algorithm: Each activity has a unique tag (ex. 'A' , 'B' , ...)

Create AcitivityManager class which holds list of activities (stack of Pairs - Each pair holds it unique tag and it activity)

Each new activity would be added to this manager by calling it from 'onCreate' method (You can use a singleton or send a local notification). In this way you would get the A -> B -> C -> D -> E -> F.

When the user would press the 'C' button , the manager would pop all the above activities by the following mechanism:

From the 'onClick' button , call to 'popToTag' method with the following arguments : it tag and destination tag.

Store the destination tag locally , and then call to 'popActivity' method.

'popActivity' method would test the peek of the stack until the target tag would arrived.

Each time 'popActivity' method would call to the 'finish' method until the target tag would be fetched.

public void popToTag(String currentTag , String targetTag){
   this.targetTag = targetTag;
   if (!currentTag.equals(targetTag)){
     popActivity();
   }
}

public void popActivity(){ 
  if (!stack.isEmpty()){
    if (targetTag != null){
       if (!stack.peek().pair.tag.equals(this.targetTag)){
         Activity top = stack.top().pair.activity; //get the top activity
         top.finish(); //pop the activity from Android 'back stack' 
         stack.pop();  //pop the top activity from the stack
       }
    }else{
      stack.pop();  //pop the top activity from the stack
    }      
  }
  targetTag = null;  //reset the targetTag
} 

The next stage is to call again to 'backStack' from 'onDestroy' method

public void onDestroy(){
   super.onDestroy();
   AcitivityManager.getInstance().popActivity();
}
  • Please notice that when the user is pressing on the back button the onDestroy would also be called and also the AcitvityManager but with targetTag equal to null , as expected.

Upvotes: 1

Ashish Shukla
Ashish Shukla

Reputation: 1047

I think you should use

android:noHistory="true"

for all previous activities in your Manifest, in your example activity's like D and E.

So they will not be on the History Stack and you can come back directly from

Activity F->C->B->A->Exit

Upvotes: 0

Fustigador
Fustigador

Reputation: 6459

Short answer: You don't have access to the Activities stack. They are managed entirely by Android internally.

BUT

When back pressed on an Activity, you could add an extra to the intent indicating which Activity is coming from, and act accordingly.

Upvotes: 0

Dusan Dimitrijevic
Dusan Dimitrijevic

Reputation: 3219

You could call after launching activity this method finish() and when you navigate to C activity, there you can call overridden method onBackPressed() and launch activity B, and from B also call an method onBackPressed() and launch activity A.

Upvotes: 0

Related Questions