Android - Intent controls. The old intent is resubmitted if the user opens the application from the task manager

I have a problem. When I call finish() , the activity of the method is saved in the task manager, and if the user reloads it from the task manager, my activity gets the old intention. If this intention was sent from a push notification, I had an undesirable reaction: my intention was to start the process with the push notification data.

How to properly manage the behavior of push notifications in my activity in order to avoid an incorrect activity state?

My application receives a push notification and a form in anticipation of the intention of the reaction when pressed:

  final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); int defaultOptions = Notification.DEFAULT_LIGHTS; defaultOptions |= Notification.DEFAULT_SOUND; Intent intentAction = new Intent(context, MainActivity.class); intentAction.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); intentAction.putExtra(BUNDLE_COLLAPSE_KEY, data.getString(BUNDLE_COLLAPSE_KEY)); intentAction.setAction(CUSTOM_ACTION); final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, PendingIntent.FLAG_UPDATE_CURRENT); int notificationFlags = Notification.FLAG_AUTO_CANCEL; final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_splash) .setContentTitle(context.getResources().getString(R.string.app_name)) .setContentText(data.getString(BUNDLE_PUSH_CONTENT_DATA)) .setContentIntent(pendingIntent) .setAutoCancel(true) .setDefaults(defaultOptions) .getNotification(); notification.flags |= notificationFlags; mNotificationManager.notify(NOTIFICATION_ID, notification); 

after the user came to the application from push, the application will obviously get the intention using CUSTOM_ACTION and do some work:

 private void intentProcess(Intent intent) { boolean customAction = intent.getAction().equals(GCMPushReceiver.CUSTOM_ACTION); if (customAction) { //push reaction, do some staff with intent } else { //no push reaction, user just open activity } } 

I call the intentProcess method from onCreate and from onNewIntent :

 public class MainActivity extends FragmentActivity { //this case if my app closed and user tap on push @Override protected void onCreate(Bundle savedInstanceState) { /* ... */ intentProcess(getIntent()); } //this case if my app opened and user tap on push @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); intentProcess(intent); } } 

Manifest activity declaration:

  <activity android:name=".ui.activity.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
+8
android android-intent
source share
1 answer

Try using

 int id= NotificationID.getID(); final PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intentAction, PendingIntent.FLAG_UPDATE_CURRENT); // rest of your code notificationManager.notify(id, notification); 

Add new class

 public class NotificationID { /** * An AtomicInteger is used in applications such as atomically incremented * counters, and cannot be used as a replacement for an Integer. However, * this class does extend Number to allow uniform access by tools and * utilities that deal with numerically-based classes. */ private final static AtomicInteger c = new AtomicInteger(0); /** * Method to the automatically incremented int value. * * @return */ public static int getID() { return c.incrementAndGet(); } 

}

Each time a generated notification creates a new intent with a different identifier.

If you do not want your activity to be displayed / added to the task manager, add these lines to the AndroidManifest activity tag.

 android:excludeFromRecents="true" android:noHistory="true" 
+1
source share

All Articles