Wednesday, December 14, 2011

[android-developers] How to ensure my root Activity is never duplicated?

I'm writing a board game that follows a common pattern: main Activity
is a list of games, and if you tap on one it opens into a second
Activity in which the game is played. You close one game to play
another. Games can also be launched over the network, which happens
when the user goes to a .php page in the browser that redirects to a
custom URL scheme for which one of my Activities has an intent-filter.

I want to ensure three things

* If user opens the app, then launches a game into an Activity on top
of the list Activity, then taps the home button and launches the app
again, he'll be taken back to the second Activity with the game in
progress, just as it was. (This is standard behavior on Android.)

* If he browses to the .php page and triggers a new game, the app will
display the new game in its own Activity on top of the list
Activity, first closing any other game Activity that might be open.

* If this happens while the app is already running, whether in the
same task as the browser or not, the new game will be launched on
top of the existing game list Activity -- no new game list Activity
will be created.

This last one's the problem. The way things are working now, with the
game list Activity's launchMode set to singleTop, if the game's
already running in a different task and the user browses to the .php
page that triggers new game creation, a second instance of the game
list Activity is created with the new game Activity on top of it.
This is fixed if I make launchMode be singleTask, but then my first
goal fails: If the user hits the home button while a game is open then
relaunches the app it comes back not to the existing game Activity but
to a new root game list Activity.

I'm beginning to work toward a solution that has the root Activity
registering with a static class so that I can tell it's running (and
can have it finish() if the OS insists on launching a duplicate), and
the
child game Activities registering with their parent so the parent can
kill them if it needs to. But it's going to be pretty complex, and
probably racy. It seems this scenario should have been anticipated,
that there's probably a simple way to do it using launch modes and
Intent flags I'm not finding. Suggestions?

Thanks,

--Eric

--
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+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment