9

I am running into a peculiar issue with an app which has multiple Activities. I have a screen manager class that is bound to a service. The service polls a server for data. The screen manager starts Activity A, B, or C, based on the data. It will also allow the user to select to display the other Activities or it may swap out an Activity automatically based on new data from the service. Currently all the navigation works great and if the user presses Home, the applicable Activity comes back to the foreground when the user presses the apps icon from the Android home screen or the recently run apps list.

I then had to implement a new feature to display an icon and notification. I first implemented this by only displaying the notification when the Activities were no longer visible by setting it in each Activities onPause. Worked like a charm and gave the user a third option to redisplay the app after Home button press. However if the data from the server (when the app is not displayed) causes the screen manager to update the Activity to be displayed I have issues. I think my Activity stack is getting screwed up. I have since tried a little different model where the screen Manager handles the notifications and always displays the notification, updating it with a new Intent whenever an Activity is updated, but its still not cutting it.

When the app is minimized and the data changes I can see the screen manager call the startActivityForResult on the new Activity and it seems like Android knows we are minimized and does not display the Activity. I can then also the screen Manager call to finish on the old top Activity.

Each Activity currently has the flag set to singleTop in the manifest and FLAG_ACTIVITY_SINGLE_TOP in the code. When the data hasn't changed on the server, I pull the app back up using any of the above I get what I expect and the Activity's onNewIntent is called. However when the data has changed and the user calls up the app via the notification it calls the Activities onCreate. The Activity does come up and runs, but then pressing back takes me to what seems like another instance of the same Activity instead of exiting. I just took a closer look at my logcat and I see 2 calls to the Activity's onResume, but then no doubles after that.

I feel like maybe I'm just missing something simple like another intent flag on the notification's Intent? Thanks for any ideas!

bursk
  • 1,647
  • 7
  • 25
  • 39

1 Answers1

7

What is killing me is the data from the server changing when not displayed.

I have no idea what this means.

I feel like maybe I'm just missing something simple like another intent flag on the notification's Intent?

I used Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP to deal with the "takes me back to what seems like another instance of the same Activity" problem, but in my case, I had a single-activity application.

The documentation for FLAG_ACTIVITY_CLEAR_TOP also says:

"This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager."

There is also FLAG_ACTIVITY_REORDER_TO_FRONT:

"For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified."

Perhaps one of those patterns fits your need.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • "What is killing me is the data from the server changing when not displayed." Sorry, shouldn't have used slang. What I was trying to say is that it was working great - except 0 when the screen manager is told to change the displayed Activity, and I found that very frustrating. Thanks for your advice I'll retry using that additional flag. – bursk Nov 09 '10 at 00:55
  • thanks for the idea, but setting that flag doesn't seem to change the behavior. – bursk Nov 09 '10 at 16:19
  • I think I discovered why your suggestion did not appear to be working. I think that another background thread was preventing the first call to start the activity from executing so when the intent from the notification fires it doesn't see the activity on the stack top. So the Activity creates itself and displays. Then when I dismiss it using the back button, it is removed from the stack. – bursk Nov 14 '10 at 15:10
  • @cbursk: I know this is ancient history now....but do you recall whether you found a way to stop the background task from inhibiting the startActivity? I am facing this same issue with a Runnable I'm trying to use to put a short delay before the startActivity kicks off. thx, Pete – PeteH Feb 06 '13 at 01:59
  • @PeteH: sorry, I've long since moved onto another project and don't recall. Best of luck – bursk Feb 11 '13 at 21:14