Resume application and stack from notification

I want to resume my application from the notification in the status bar in the same way as when the user clicks the icon in the launch bar.

That is: I want the stack to be in the same state as before the user left it.

The problem with setting up a pending notification intent is that it always focuses on a specific action. I do not want it. I need to resume the application in the same way as the launcher does.

So, if the user is in action A, I want to resume action A. If he started action B from action A, then I want B to be displayed when the user deletes the notification, and the stack to be restored, that A resumes when the user deletes the back button in B.

There are several other questions with similar names, but not a single issue affects my issue.

+48
android
Mar 31 '11 at 15:35
source share
7 answers

Just use the same intent filters as Android when it launches the application:

final Intent notificationIntent = new Intent(context, YourActivity.class); notificationIntent.setAction(Intent.ACTION_MAIN); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

Since the Intent you created to open your Activity in the notification panel is the same as the Android used to launch your application, the previously created Activity will be displayed instead of creating a new one.

+123
Mar 31 '11 at 16:14
source share

In situations where you do not want / cannot hardcode the start activity, this solution works

 Intent i = getPackageManager() .getLaunchIntentForPackage(getPackageName()) .setPackage(null) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0); return new NotificationCompat.Builder(context) ... .setContentIntent(pendingIntent) .build(); 

The setPackage (null) part was crucial in my case, since without it the application would not resume until the previous task. I compared my intention with the intent from the Android launcher and noticed that pkg was not installed there, so I came up with removing the package name from the intent.

My special situation was that a notification was created in the library, so I could not know what the launch activity would be.

+6
Feb 02 '17 at 12:48 on
source share

I also had the same problem and tried to solve the problem as @GrAnd answer:

 final Intent notificationIntent = new Intent(context,YourActivity.class); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); 

This is beyond doubt, but the problem is that you set your intent as ACTION_MAIN. Then you cannot install any kit for the intention. I mean that your primitive data will not be obtained from the target activity, because ACTION_MAIN cannot contain any additional data.

Instead, you can simply configure your actions as a singleTask and name your intent normally without setting ACTION_MAIN and without getting the intent from the onNewIntent () method of the target activity.

But get lost if you call, super.onNewIntent (intent); a second instance of the action will be created.

+3
Apr 24 '15 at 12:41
source share

Creating an activity, and then set the categories and the corresponding flags ... That’s how it worked for me, I had to do it that way, because I did it to support Api lvl 8

 intent.addCategory(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.setClass(this, YourActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT| Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

and in AndroidManifest

 android:launchMode="singleTask" 

So the trick was Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT along with the line set in the manifest.

Hope this helps other people.

+2
Jul 27 '14 at 21:59
source share

It’s very simple to open the manifest file and set the singleTop launch mode attribute to your activity attribute

0
Nov 11 '12 at 18:28
source share
 <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.REORDER_TASKS" /> private void bringApplicationToForeground(){ ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { List<ActivityManager.AppTask> tasksList = am.getAppTasks(); for (ActivityManager.AppTask task : tasksList){ task.moveToFront(); } }else{ List<ActivityManager.RunningTaskInfo> tasksList = am.getRunningTasks(Integer.MAX_VALUE); if(!tasksList.isEmpty()){ int nSize = tasksList.size(); for(int i = 0; i < nSize; i++){ if(tasksList.get(i).topActivity.getPackageName().equals(getPackageName())){ am.moveTaskToFront(tasksList.get(i).id, 0); } } } } } 
-one
Oct. 12 '15 at 12:48
source share

There may be an easier way, but you can store the data in the sqlite database and whenever you restart the application, all the states you saved are in the database that you can get and set your values ​​to what they need .

-four
Mar 31 '11 at 16:09
source share



All Articles