How to make activity reorder to front "for real"?

1.5k Views Asked by At

I use FLAG_ACTIVITY_REORDER_TO_FRONT to try to switch between activities. Since this is buggy (sometime hide when back), so I try to dive into it.

The FLAG_ACTIVITY_REORDER_TO_FRONT not really make the activity move to top, as I can see from adb, the activities order never changed:

xb@dnxb:~/Downloads$ adb shell dumpsys window windows | grep -n 'Window #'
2:  Window #13 Window{f7d02ea u0 NavigationBar}:
31:  Window #12 Window{fdfb1ed u0 com.android.systemui}:
56:  Window #11 Window{b626be1 u0 StatusBar}:
84:  Window #10 Window{bd80846 u0 KeyguardScrim}:
109:  Window #9 Window{f127fe9 u0 AssistPreviewPanel}:
134:  Window #8 Window{f5a5c17 u0 DockedStackDivider}:
162:  Window #7 Window{5170bb1 u0 com.android.systemui}:
188:  Window #6 Window{201fb75 u0 InputMethod}:
217:  Window #5 Window{7555f63 u0 com.stackoverflow/mythirdActivity}:
246:  Window #4 Window{7e3a230 u0 com.stackoverflow/mySecondActivity}:
273:  Window #3 Window{7a4b856 u0 com.stackoverflow/myFirstActivity}:
300:  Window #2 Window{cc1c79f u0 com.huawei.android.launcher/com.huawei.android.launcher.unihome.UniHomeLauncher}:
329:  Window #1 Window{f2df7b0 u0 com.android.systemui/com.android.systemui.recents.RecentsActivity}:
357:  Window #0 Window{5a3ffdc u0 com.anifree.engine.Wallpaper}:

FLAG_ACTIVITY_REORDER_TO_FRONT only change the "focus status" from this:

 mDrawState=NO_SURFACE       mLastHidden=true

to this:

  mSurface=Surface(name=com.stackoverflow/mySecondActivity)
  Surface: shown=true layer=21020 alpha=1.0 rect=(0.0,0.0) 1080.0 x 1920.0 blurRadius = 0 blurRound = (0,0) blurAlpha = 0.0 blurRegion = null blurBlank = null
  mDrawState=HAS_DRAWN       mLastHidden=false

But if I play around by press home button, I can see the launcher window able to do the "real moving" to top:

xb@dnxb:~/Downloads$ adb shell dumpsys window windows | grep -n 'Window #'
2:  Window #13 Window{f7d02ea u0 NavigationBar}:
31:  Window #12 Window{fdfb1ed u0 com.android.systemui}:
56:  Window #11 Window{b626be1 u0 StatusBar}:
84:  Window #10 Window{bd80846 u0 KeyguardScrim}:
109:  Window #9 Window{f127fe9 u0 AssistPreviewPanel}:
134:  Window #8 Window{f5a5c17 u0 DockedStackDivider}:
162:  Window #7 Window{5170bb1 u0 com.android.systemui}:
188:  Window #6 Window{201fb75 u0 InputMethod}:
217:  Window #5 Window{cc1c79f u0 com.huawei.android.launcher/com.huawei.android.launcher.unihome.UniHomeLauncher}:
248:  Window #4 Window{f2df7b0 u0 com.android.systemui/com.android.systemui.recents.RecentsActivity}:
217:  Window #5 Window{7555f63 u0 com.stackoverflow/mythirdActivity}:
246:  Window #4 Window{7e3a230 u0 com.stackoverflow/mySecondActivity}:
273:  Window #3 Window{7a4b856 u0 com.stackoverflow/myFirstActivity}:
357:  Window #0 Window{5a3ffdc u0 com.anifree.engine.Wallpaper}:
xb@dnxb:~/Downloads$ 

Since hide/unhide to background to move window is possible, this make me wonder is it possible make mySecondActivity on top of mythirdActivity programmatically, like this:

Window #4 Window{7e3a230 u0 com.stackoverflow/mySecondActivity}:
Window #5 Window{7555f63 u0 com.stackoverflow/mythirdActivity}:
Window #3 Window{7a4b856 u0 com.stackoverflow/myFirstActivity}:

Note that I'm not talking about new instance, I mean the same window id Window{7e3a230 move to top without change the window id.

Is it possible ? Or I misunderstand it ?

2

There are 2 best solutions below

6
On

Ensure you don't set FLAG_ACTIVITY_CLEAR_TOP, because FLAG_ACTIVITY_REORDER_TO_FRONT flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.

Below code snippet won't create a new instance, i.e. a new WindowId for your case, and will bring up the activity to top if you already have it in your activity history.

Intent i = new Intent(context, YourActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
0
On

I figured out I must use Intent.FLAG_ACTIVITY_MULTIPLE_TASK(with correct manifest launcheMode) to make it multiple task so the window(s) in same task stack can be reorder as a group to top/below of other task while retain same window id. moveTaskToBack(true); also can do this but I noticed it not able to move back to foreground.