I did finally get the notification icon to successfully return to my
activity which started the service with the activity stack intact.
In the manifest file I defined a new task affinity that is used for
all the activities related to this part of the application.
The two activities from the activity stack that are most relevant are
the root "Library" activity, and the "AddAudioFilesProgress" activity
that the service is trying to get back to using a pending intent. In
the manifest file these activities are now defined as:
<activity
android:name="com.amblingbooks.player.Library"
android:label="@string/book_list"
android:taskAffinity=":library"
android:clearTaskOnLaunch="false"
android:alwaysRetainTaskState="true"
android:allowTaskReparenting ="true"
android:launchMode="singleTop" />
<activity
android:name="com.amblingbooks.player.AddAudioFilesProgress"
android:label="@string/activity_add_audio_files_progress"
android:taskAffinity=":library"
android:clearTaskOnLaunch="false"
android:alwaysRetainTaskState="true"
android:allowTaskReparenting ="true"
android:launchMode="singleTop" />
One thing that was not intuitive and which I still don't understand
was that if I set the launchMode of the root "Library" activity to
singleTask, then when the notification icon is used to start the
pending intent for the "AddAudioFilesProgress" activity, the activity
stack gets cleared and I end up looking at the "Library" activity.
When I switched the launchMode of the root activity to singleTop
suddenly my activity stack was left intact and I was getting back to
the "AddAudioFilesProgress" activity, with the activity which had
started it still included in the activity stack. The launchMode of
the root activity changed the behavior which all the other activity
settings (like clearTaskOnLaunch=false and alwaysRetainTaskState=true)
did not seem to effect.
I still don't fully understand the way the Android manages
activities. My application currently has 26 activities in the
manifest file and there are not any of them which should ever have
multiple instances running. The default Android behavior of starting
a new instance of an activity for each new intent doesn't work well
for me. It makes me wonder what percentage of applications really
want to have all those duplicate instances running. The
singleInstance and singleTask launch modes are the only ones that are
described in the documentation as not starting multiple instances,
however the singleInstance launch mode does not support an activity
stack, and the singleTask launch mode has unwanted side effects like
clearing my entire activity stack when using a pending intent from a
notification icon. This behavior was especially confusing because the
intent that was being launched was the "AddAudioFilesProgress" intent
which was not the root activity, but the stack was cleared and the
root "Library" activity was the one that was displayed.
A side effect that I hadn't expected from defining multiple tasks with
activity task affinity was that when the home button is held down to
display recent activities I started seeing multiple instances of my
application icon with each instance returning to the activities on a
different task in my application. I found I could modify the icon
shown per activity, but I didn't need them all to be showing, so I set
some of them to android:excludeFromRecents="true". I was going to
leave two of the tasks showing, but the root activity for the other
task still has a lunchMode of singleTask and for some unknown reason
if I didn't specify android:excludeFromRecents="true" on that activity
it caused problems with not getting back to my activity stack on
the :Library task from the notificiaton icon pending intent again.
I have it working now, but it was from sheer trial and error as this
behavior is certainly not explained by the Android documentation
anywhere.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en