Why does my app get minimized without an obvious reason?

1k Views Asked by At

TL;DR My app gets minimized (as if the home-button was pushed) when I tap the navigation icon in my toolbar after going from an activity to another one and back.

Detailed flow of actions:

  • Open app, Activity1 comes up
  • Tap navigation icon (drawer opens normally, I can close and open it any amount of times at this stage, by pressing the navigation icon)
  • Tap a drawer menu item which starts Activity2 with flag FLAG_ACTIVITY_REORDER_TO_FRONT
  • After Activity2 finishes loading, tap the navigation icon, drawer opens normally
  • Tap the drawer menu item which starts Activity1 with the flag FLAG_ACTIVITY_REORDER_TO_FRONT (at this point this Activity exists so is brought to front)
  • After Activity1 is brought to front, tap navigation icon again to open the drawer
  • Drawer starts sliding out from the left but app minimizes before it's out completely

I know the app is minimized because when I open it, Activity1 is on the screen with an open drawer

There's no Exception or log (level is at Verbose) and this happens only on a OnePlus Two (The app works fine on a Nexus 4)

1

There are 1 best solutions below

0
On

In my case I need to switch between 2 distinct stack. I use FLAG_ACTIVITY_REORDER_TO_FRONT to handle startActivity and finish().

But I have the same problem like you, it get minimized in one case:

Activity Class A (instance 1) -> open new Activity Class A (instance 2) -> Activty Class B -> switch to Activity Class A (instance 2) by FLAG_ACTIVITY_REORDER_TO_FRONT, then FLAG_ACTIVITY_REORDER_TO_FRONT to back to (instance 1) will minimized (go to background), but if I make it foreground back it did finish(), it just go to background by unknown reason.

It only happen if two class A instances on the stack continuously. I check the stack by adb shell dumpsys window windows | grep 'Window #'

After I tried a lot of FLAG combination with no succeed, I come out with a hack idea, i.e. create a Distinct activity class.

While I already able to detect this is the same class on the stack (use registerActivityLifecycleCallbacks to push/pop the activity list stack), then when I want to back I simply call the Distinct class and make it finish() immediately in onCreate(). Then on the finish() code will FLAG_ACTIVITY_REORDER_TO_FRONT the class A instance 1. Since the Distinct class is not the same class of A, then it able to FLAG_ACTIVITY_REORDER_TO_FRONT the instance 1 and close instance 2 just fine.

[UPDATE]

This answer actually buggy, but eventually I figure out the better way and write the answer here.