views:

232

answers:

3

I am developing an app with numerous Activities. I would like to create a persistent notification that (more or less) says, "AppName - Return to AppName" that will be present whenever my background services are running. Creating and disposing of the notification was no problem.

Now, the user could be on any of several screens/Activities, leave the application, then want to re-enter the app via the notification. The problem is, the notification must have an intent, which launches a predetermined Activity. I want the notification to re-enter the app in whatever Activity is at the top of the history stack.

My first attempt at an ugly workaround was to make an activity (let's call it "returnFromNotify") whose only job was to "finish" itself in it's "onCreate". The notification would open "returnFromNotify" in the scope of the applications history, which would then immediately remove itself, sending the user back to the previous history state in the application stack. This seems to work... unless the user has used "back" to completely back out of the app. Then, when they hit the notification, "returnFromNotify" loads, then finishes, sending them back out to the home screen (as there are no activities in the history stack for the app).

I considered trying to detect if there was anything in the history stack before "returnFromNotify", and if not, fire up my main Activity. I can't seem to find a way to do this, either.

Any input or suggestions for a Java/Android novice? FYI, My primary history is with script-based languages.

+2  A: 
Slobaum
+1  A: 

I guess there is no easy way to do this but instead of adding a counter in the mainActivity I would extend Application:

Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.

I would mantein the logic there and have a method like:

public Intent getIntentForLastActivityShown();

to be called when the notification item is clicked.

Macarse
How would you then use getIntentForLastActivityShown() ? Wouldn't an easier way be to keep a handle on the notification in the Application class and then everytime you start a new intent call setLatestEventInfo from the notificaiton and update the pendingIntent to the current visible activity.
schwiz
+1  A: 

My first approach would be to use SharedPreferences and store a key value pair called something like lastDisplayedActivity. Then in each Activity's onResume (and possibly `onCreate') you would have a line like this:

sharedPreferences.edit().putInteger("lastDisplayedActivity", ReturnFromNotify.THIS_ACTIVITY_NAME);

In other words, you store an application-wide variable indicating which activity was last displayed. Then you just grab this variable from SharedPreferences and launch the corresponding activity.

Computerish
That's not a bad idea, but it would require me to add code to every activity's life-cycle (which I would rather not do). Also, would launching the corresponding activity maintain the state of the previous activity instance, or would it make a new instance of that activity? I really need it to retain whatever state they were in.
Slobaum
It may restore the state automatically in some cases, but if your Activity is not currently being displayed, Android *may kill it at any time* to reclaim extra memory. In other words, if you want to be sure that you save the Activity's state, you must store it somewhere else. There are numerous ways to do this, but if you have a fair amount of data I would recommend using an SQLite database:http://developer.android.com/guide/topics/data/data-storage.html#db
Computerish