Android Activity Stack is not working as stated in the docs - last activity in task stack not shown

前端 未结 3 1932
北荒
北荒 2020-12-16 09:12

According to Android docs:

http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html

\"When the user leaves a task by pressing the Hom

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-16 09:22

    This is a bug in android's platform:

    http://code.google.com/p/android/issues/detail?id=2373

    The workaround is, to place this in the onCreate method of your main Activity:

    if (!isTaskRoot())
    {
        final Intent intent = getIntent();
        final String intentAction = intent.getAction(); 
        if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN))
        {
            Log.w(LOG_TAG, "Main Activity is not the root.  Finishing Main Activity instead of launching.");
            finish();
            return;       
        }
    }
    

    as extracted from:

    How to prevent multiple instances of an activity when it is launched with different intents

    ...spent 3 days looking out for this.

提交回复
热议问题