0

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 ?

林果皞
  • 7,539
  • 3
  • 55
  • 70
  • 1
    I'm not sure what you think this flag does, but it seems like you're confused. If you launch an activity with this flag sent in the intent, it means instead of launching a new version of the activity it will display an existing one, if one exists. It has nothing to do with window order or the home button – Gabe Sechan Dec 21 '18 at 02:39
  • @GabeSechan Is there any flag reorder the window ? – 林果皞 Dec 21 '18 at 03:14
  • 1
    What you want isn't a feature, to the best of my knowledge. It would require reordering the stack, which isn't something Android does. – Gabe Sechan Dec 21 '18 at 03:20

2 Answers2

0

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);
shizhen
  • 12,251
  • 9
  • 52
  • 88
  • 1
    I know FLAG_ACTIVITY_REORDER_TO_FRONT can see on top, but the reason I said this is not real is because if I keep reorder acitivty A ->(create new) B -> (new) X -> (new) Y -> (reorder) B -> press Back -> this will hide the app before able to see A. And the adb shows this is not really reorder the window layer. – 林果皞 Dec 21 '18 at 02:58
  • You new task stack should be A,X,Y,B, and press back, B dismissed, but you should see A,X,Y, with Y on top. Check your code logic if you have dismissed your A,X,Y activities when your start the next Activity. – shizhen Dec 21 '18 at 03:29
  • The A,X,Y still there, I can click Home button to make it foreground again, and I can navigate them by reorder, they didn't dismiss as shown in adb too. I think you can do the test, I make a simple app to test and the result are same. – 林果皞 Dec 21 '18 at 03:30
  • You task flow and stack flow looks nothing special, try to review your code may help. – shizhen Dec 21 '18 at 03:38
  • Just curious about how you declare your activities inside manifest file, how many launchers? Do you use any modes related to different `tasks`? – shizhen Dec 21 '18 at 06:10
  • I fixed my problem already (https://stackoverflow.com/a/53919137/1074998), thanks anyway. – 林果皞 Dec 25 '18 at 03:40
0

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.

林果皞
  • 7,539
  • 3
  • 55
  • 70