Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notification to restore a task rather than a specific activity?

I have a foreground service that keeps a connection open with the server as long as the user is logged into the application. This is so that the connection is kept alive and can receive messages directly from the server even when the application has been sent into the background by the user pressing Home.

The application has a number of Activities, any of which could be the active one when it is sent into the background.

I would like to allow the user to click on the notification to restore the current Activity. I understand how to restore a particular activity, but wondered if there is a way to restore the last Activity that the user was on? Of course I could keep track of the the last one, and then call that from the Notification callback, but thought there might be a way at a task level?

Thanks for any advice you can offer.

like image 554
Brad Avatar asked Jul 04 '11 20:07

Brad


People also ask

How do I go back to start an activity from a notification?

When you start an activity from a notification, you must preserve the user's expected navigation experience. Tapping Back should take the user back through the app's normal work flow to the Home screen, and opening the Recents screen should show the activity as a separate task.

What function do you call to stop an activity and go back to the previous activity?

You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.


1 Answers

What you need is just a simple Activity that does nothing. Here is an example:

public class NotificationActivity extends Activity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         // Now finish, which will drop the user in to the activity that was at the top         //  of the task stack         finish();     } } 

Set up your notification to start this activity. Make sure that in the manifest the task affinity of this activity is the same as the task affinity of the other activities in your application (by default it is, if you haven't explicitly set android:taskAffinity).

When the user selects this notification, if your application is running, then the NotificationActivity will be started on top of the topmost activity in your application's task and that task will be brought to the foreground. When the NotificationActivity finishes, it will simply return the user to the topmost activity in your application (ie: wherever the user left it when it went into the background).

This won't work if your application isn't already running. However, you have 2 options to deal with that:

  1. Make sure the notification isn't present in the notification bar when your application is not running.

  2. In the onCreate() method of the NotificationActivity, check if your application is running, and if it isn't running call startActivity() and launch your application. If you do this, be sure to set the flag Intent.FLAG_ACTIVITY_NEW_TASK when starting the application so that the root activity of the task is not NotificationActivity.

like image 58
David Wasser Avatar answered Sep 23 '22 03:09

David Wasser