I have two screens, A and B. Screen A has a button that launches Activity B and Screen B has a button that launches Activity A.
The expected behavior is that if the activity is available in back stack, that should be reordered to the front without creating a new one. To achieve so, I added flag FLAG_ACTIVITY_REORDER_TO_FRONT on both activity's click event. Here is the listener code.
Button btn=(Button)findViewById(R.id.button);
final Activity activity=this;
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent=new Intent(activity,ScreenB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
});
This is what's expected. A is the first screen (after splash, so A is not a launcher screen).
- Stack is "A(Top)"
- Button on screen A is clicked. Screen B is (Newly created) launched. Stack is "A->B(Top)"
- Now Button on screen B is clicked. Screen A is (Reordered to front) launched. Stack is "B->A(Top)"
At this point of time, if back button is pressed, A should be removed and B should be visible (Theoretically). But what happens is, on back button App is no more visible and Mobile device's home launcher appears. If I reopen app from recent apps, it opens at B. Which means B must be in the stack when back button was pressed but instead of showing it, app went out and showed the mobile launcher.
What else should I do to get expected behavior?
I added screen C (3rd screen) for testing
now I have 3 screens. All has two buttons to launch other two.
- Stack is "A(Top)"
- From screen A, ButtonB is clicked, B is launched (New), stack A->B(Top)
- From screen B, ButtonC is clicked, C is launched (New), stack A->B->C(Top)
- From screen C, ButtonA is clicked, A is launched (Reordered), stack B->C->A(Top)
- Back button pressed, A was removed, app went out, mobile launcher visible. stack B->C(Top)
- Opened app from the recent apps list. C was visible. Stack B->c(Top)
- Back was pressed, C was removed and B was visible. Stack B(Top). At this time it behaved correctly.