Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, what Intent flags are recommended for Activities started by Notifications

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!

like image 609
bursk Avatar asked Nov 08 '10 23:11

bursk


People also ask

What do the flags mean in Android?

The flags may instruct the Android system how to launch an activity (for example, which taskthe activity should belong to) and how to treat it after it's launched (for example, whether it belongs in the list of recent activities). For more information, see the setFlags()method. Example explicit intent

What is flag_activity_new_task in Android?

Android Intent FLAG_ACTIVITY_NEW_TASK If set, this activity will become the start of a new task on this history stack. If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to.

What is the use of the activity launch flag?

This flag is generally used by activities that want to present a "launcher" style behavior: they give the user a list of separate things that can be done, which otherwise run completely independently of the activity launching them.

How do I use flags in startactivity?

There are flags which you can use in this time depending on your requirement. Here is how they work : FLAG_ACTIVITY_CLEAR_TASK - If set in any intent that is passed to your startActivity (), it will cause any existing task that would be associated with the activity to be cleared before the activity is started.


1 Answers

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.

like image 175
CommonsWare Avatar answered Sep 22 '22 07:09

CommonsWare