0

Is there a possibility to reopen a specific activity from the stack? So say I open activity a, then b, then c. I do not finish a and b while starting new activities. If a push my back-button without overriding it, I would go to B now of course.

But I want to give a button or maybe the back button to open A, or B independent from its location in the stack. This is kind of achievable by finishing the activities (if I would finish b, and press back button from C, I would go to A). But some of my activities I'd rather not finish.

I researched but could not find how to achieve this. Is this possible?

Of course there would be a check needed if the activity is active, and if it is then reopen it or else open it.

1

2 Answers 2

7

You will obviously have to figure out what logic you want to know which Activity to open but this should achieve what you are looking for

@Override
public void onBackPressed()
{
    super.onBackPressed();
    Intent intent = new Intent(CurrentActivity.this, ActivityYouWant.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
    startActivity(intent);
}

Let me know if this isn't what you were looking for.

Flag

2
  • Thanks, worked like a charm. No idea how i could have missed this is my research, but this was exactly what i was looking for.
    – Jasper
    Commented Apr 8, 2013 at 17:25
  • Intents have quite a bit to them which makes it easy to overlook things until you work with them more. Glad I could help
    – codeMagic
    Commented Apr 8, 2013 at 17:50
1

the way codeMagic suggested you may produce more Activities in the Activity stack then you need. This may leed to Memory Leaks or other bad behavior. you should take a look at:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

or even better finish() your Activities.

Not the answer you're looking for? Browse other questions tagged or ask your own question.