Using FLAG_ACTIVITY_REORDER_TO_FRONT to switch among persistently running UI activities leads to “no window focus” error

后端 未结 1 1471
孤街浪徒
孤街浪徒 2021-01-05 00:56

My objective is to keep two UI activities alive and to switch back and forth between them at will without having to kill/restart either of them. But there is a serious side

相关标签:
1条回答
  • 2021-01-05 01:16

    The workaround provided in https://code.google.com/p/android/issues/detail?id=63570#c15 worked pretty well for me to resolve the issue of Activity not getting window focus:

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if ((intent.getFlags() | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) > 0) {
            if (android.os.Build.VERSION.SDK_INT >= 19 && !isTaskRoot()) {
                ActivityManager tasksManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                tasksManager.moveTaskToFront(getTaskId(), ActivityManager.MOVE_TASK_NO_USER_ACTION);
            }
        }
    }
    

    This will also need the permission in AndroidManifest.xml

    <uses-permission android:name="android.permission.REORDER_TASKS" />
    

    I actually run into this issue in Espresso test getting such error:

    java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong.

    0 讨论(0)
提交回复
热议问题