Resumption of notice

I have a notification in the status bar for my application:

Notification notification = new Notification(R.drawable.icon, null, System.currentTimeMillis()); Intent notificationIntent = new Intent(this.parent, MainActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this.parent, 0, notificationIntent, 0); ... notification.flags = Notification.FLAG_ONGOING_EVENT; mNotificationManager.notify(NOTIFICATION_ID, notification); 

The problem is that when you click the home button from the application (by clicking on the background), click on the notification in the list accessible from the status bar and it will launch a new copy of the action. All I want to do is resume the application (for example, when you release the home button and click on the application icon). Is there any way to create an intention to do this?

+35
java android android-activity notifications
source share
8 answers

I solved this problem by changing the launchMode my activity to singleTask in the androidManifest.xml file.

The default value for this property is standard , which allows you to run any number of instances.

"singleTask" and "singleInstance" can only begin the task. They are always at the root of the activity stack. In addition, a device can only hold one instance of activity at a time - only one such task. [...]

The "singleTask" and "singleInstance" modes also differ from each other only in one respect: the "singleTask" activity allows other actions to be part of their task. He always lies at the heart of his task, but other actions (necessarily β€œstandard” and β€œsingle”) can be included in this task. On the other hand, the activity of "singleInstance" does not allow other actions not to participate in its task. This is the only lesson in the task. If he starts another action, this activity is assigned to another task - as if FLAG_ACTIVITY_NEW_TASK was in intention.

You can find a detailed explanation in the Android Developers Guide .

I hope this helps

+32
source share

I had a similar problem, and the correct way to handle this is to use flags: Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP like this

 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

From the documentation, it will be:

FLAG_ACTIVITY_CLEAR_TOP

If it is set, and the launched activity is already running in the current task, instead of starting a new instance of this action, all other actions on top of it will be closed, and this intention will be delivered to (now above) the old activity as a new intention.

FLAG_ACTIVITY_SINGLE_TOP

If set, the action will not be launched if it is already running at the top of the history stack.

+1
source share

I had inconsistent behavior between devices with some intentions losing extras and both the MainActivity OnNewIntent and onCreate methods were called.

What didn't work :

Manifest:

  <activity android:name=".MainActivity" android:launchMode="singleTask"> 

Service:

  Intent notificationIntent = new Intent(this.getBaseContext(), MainActivity.class); notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); notificationIntent.putExtra("TEST", 1); 

What worked :

Manifest:

  <activity android:name=".MainActivity" android:launchMode="singleTask"> 

Service:

  PackageManager pm = getPackageManager(); Intent launchIntent = m.getLaunchIntentForPackage(BuildConfig.APPLICATION_ID); launchIntent.putExtra("TEST", 2); 
+1
source share

I just found a solution to this problem: I created the getPreviousIntent method and passed it to PendingIntent to everyone:

 private Intent getPreviousIntent(Intent newIntent) { final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); final List<ActivityManager.RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0); String myPkgNm = getPackageName(); if (!recentTaskInfos.isEmpty()) { ActivityManager.RecentTaskInfo recentTaskInfo; for (int i = 0; i < recentTaskInfos.size(); i++) { recentTaskInfo = recentTaskInfos.get(i); if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) { newIntent = recentTaskInfo.baseIntent; newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } } } return newIntent; } 
0
source share

Add this line to the appropriate activity in the manifest file of your application.

 android:launchMode="singleTask" 

eg:

 <activity android:name=".Main_Activity" android:label="@string/title_main_activity" android:theme="@style/AppTheme.NoActionBar" android:launchMode="singleTask" /> 
0
source share

You can simulate a launch intent (as if the user clicked your application icon to launch it):

 val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName) val contentIntent = PendingIntent.getActivity(context, 0, launchIntent, 0) 

This way, you do not need to mark your application as "singleTask" or "singleInstance" in the manifest (which may create other problems). Therefore, this should be the right decision.

Please note, however, that the above solution may not work directly for you, due to an alleged Android error. Discussions about android error and workaround can be found in this question .

0
source share

I use:

 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); notificationIntent.setAction(Intent.ACTION_MAIN); 

Not sure if these are the values ​​to set, but the answer is in these methods / flags.

-one
source share

You need to start your business in the same way as a launcher does. See: Resume application and stack from notification

-one
source share

All Articles