The issues surrounding this have been discussed before here, but I
have done a lot of poking around and wanted to report my findings and
a workaround I have come up with to see if anyone has any thoughts on
why this is happening and if my workaround has any holes in it.

What is happening:

(This refers to an application that has not set any launchMode
settings and so is using the defaults)

1. You launch an app from the Market or from the Installer.  This
launches the root/main activity of the application with the
FLAG_ACTIVITY_NEW_TASK flag and no categories.  Right now the
applications stack is [ A ]

2. Then you proceed to the next activity in the application. Now the
stack in this task is [ A > B ]

3. Then you press the home key and then relaunch the same application
by pressing it's icon from either the home screen or the app tray.

4. What is expected at this point is that activity B will show, since
that is where you left off.  However A is shown and the tasks stack is
[ A > B > A ]  This second instance of A is launched with the
following flags:  FLAG_ACTIVITY_NEW_TASK,
FLAG_ACTIVITY_RESET_IF_NEEDED, and FLAG_ACTIVITY_BROUGHT_TO_FRONT.  It
also has the android.intent.category.LAUNCHER category.

At this point, if you hit the back key, it will return you to B, as it
was when you left it.

Looking at the documentation it seems as if
FLAG_ACTIVITY_BROUGHT_TO_FRONT should only be set for activities that
use the singleTask or singleTop launchModes.  However, this
application has not set any launchModes and is therefore using the
default standard launchMode.

I assume this is not suppose to happen in this case?

So the workaround I am thinking of using is as follows.  I came up
with this because I do not want to have to ask for an extra permission
(GET_TASKS) and have to loop through running tasks on every launch.

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.

I should also note, that if the task is finished (restarting the
phone, force stopping the app, or hitting back all the way through the
stack) will fix this issue and will no longer launch it incorrectly.
It only happens if you launch the app from the installer or market and
then try to launch it from the launcher.

So in summary, why is this happening?  Is there a way to prevent it
and if not, does my workaround have any problems you can see?

Thanks much!!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to