3

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.
HBB20
  • 2,743
  • 1
  • 24
  • 35
  • Post your manifest please. – David Wasser May 23 '16 at 10:37
  • Also, are you overriding the back button for anything? – David Wasser May 23 '16 at 10:38
  • @David, I checked my manifest file based on your answer of the question related to the same topic. But after digging much, I came to know that it is a bug from 4.4.2. This is working as expected on prior versions. – HBB20 May 23 '16 at 11:32
  • 1
    Glad you were able to find out what the problem is. Please answer your own question and give links to the description of the bug. This will help other developers who also may have this problem. – David Wasser May 23 '16 at 12:30
  • Possible duplicate of [Puzzling behavior with REORDER\_TO\_FRONT](http://stackoverflow.com/questions/20695522/puzzling-behavior-with-reorder-to-front) – Edson Menegatti Dec 05 '16 at 16:39

1 Answers1

2

This is a known issue in Android which reappeared after Kitkat (4.4.2) and hasn't been fixed yet. More information about said issue can be found in this link.

This question contains a more elaborate discussion on the topic, but in general, workarounds for this exist but their effectiveness vary based on your use-case and each may introduce some unwanted consequence, such as setting the launchMode of your activity to singleInstance in your AndroidManifest.xml file.

Community
  • 1
  • 1
Edson Menegatti
  • 4,006
  • 2
  • 25
  • 40