Notifications should let the user easily get back to the previous activity

Applications that are in the background or not running can have services that send out notifications to the user letting them know about events of interest. Two examples are Calendar, which can send out notifications of upcoming events, and Email, which can send out notifications when new messages arrive. One of the user interface guidelines is that when the user is in activity A, gets a notification for activity B and picks that notification, when they press the BACK key, they should go back to activity A.

The following scenario shows how the activity stack should work when the user responds to a notification.

1. User is creating a new event in Calendar. They realize they need to copy part of an email message into this event

PUcuïe ringtorïE

Q Complets action uslr^

^ Android System " J) Rings Exiended id

SD card notifications

2. The user chooses Home > Gmail

3. While in Gmail, they receive a notification from Calendar for an upcoming meeting

4. So they choose that notification, which takes them to a dedicated Calendar activity that displays brief details of the upcoming meeting

5. The user chooses this short notice to view further details

6. When done viewing the event, the user presses the BACK key. They should be taken to Gmail, which is where they were when they took the notification

This behavior doesn't necessarily happen by default. Notifications generally happen primarily in one of two ways:

• The chosen activity is dedicated for notification only - For example, when the user receives a Calendar notification, choosing that notification starts a special activity that displays a list of upcoming calendar events — this view is available only from the notification, not through the Calendar's own user interface. After viewing this upcoming event, to ensure that the user pressing the BACK key will return to the activity the user was in when they picked the notification, you would make sure this dedicated activity does not have the same task affinity as the Calendar or any other activity. (You do this by setting task affinity to the empty string, which means it has no affinity to anything.) The explanation for this follows.

Because of the way tasks work, if the taskAffinity of the dedicated activity is kept as its default, then pressing the BACK key (in step 6, above) would go to Calendar, rather than Gmail. The reason is that, by default, all activities in a given application have the same task affinity. Therefore, the task affinity of the dedicated activity matches the Calendar task, which is already running in step 1. This means in step 4, choosing the notification brings the existing Calendar event (in step 1) forward and starts the dedicated activity on top of it. This is not what you want to have happen. Setting the dedicated activity's taskAffinity to empty string fixes this.

• The chosen activity is not dedicated, but always comes to the foreground in its initial state - For example, in response to a notification, when the Gmail application comes to the foreground, it always presents the list of conversations. You can ensure this happens by setting a "clear top" flag in the intent that the notification triggers. This ensures that when the activity is launched, it displays its initial activity, preventing Gmail from coming to the foreground in whatever state the user last happened to be viewing it. (To do this, you putFLAG ACTIVITY CLEAR TOP in the intent you pass to startActivityO).

There are other ways to handle notifications, such as bringing the activity to the foreground, set to display specific data, such as displaying the text message thread for the person who just sent a new text message.

A notification always starts an activity as a new task (that is, it puts FLAG_ACTIVITY_NEW_TASK in the intent it passes to startActivityO). This is done because interruptions to a task should not become part of that task.

0 0

Post a comment