App loses its ability to remember its stack when launched from another application

后端 未结 2 1975
不思量自难忘°
不思量自难忘° 2021-02-02 03:01

Now that I have researched this even more I am rewriting this to make it clearer. If you are looking for more info, there is some available in older edits.

What is happ

2条回答
  •  野的像风
    2021-02-02 03:29

    Here is a workaround I have come up with so far. Some other workarounds I have seen involved looking at the currently running tasks. However, I really did not want to have to ask for another permission (GET_TASKS) from the user just to make a work around.

    Please let me know if you see any holes in this.

    In the main/root activity's onCreate method, check if the intent has the FLAG_ACTIVITY_BROUGHT_TO_FRONT set and if so, call finish(). This then pops the extra instance of A off the stack [ A > B > A ] becomes [ A > B ] and from the users perspective, it launches into the activity they were expecting.

    It seems to work in all of my tests so far. My only worry is that if there is some weird case where someones launcher would always flag a launch with FLAG_ACTIVITY_BROUGHT_TO_FRONT even if the app wasn't already in a task, and therefore would completely lock them out because it would call finish() and not have anything in the stack to return to.

    --

    As requested in the comments here is how you can check if an intent a specific flag:

    int flags = intent.getFlags();
    boolean hasFlag = flags & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT == Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT;
    

    --

    Also I should note that I am still seeing this problem occur sometimes with this fix in place. It doesn't seem to be a perfect solution.

提交回复
热议问题