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
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.