Intent with old secondary in onCreate () for singleTask Activity

For example, I created a project with two actions: FirstActivity with android: launchMode = "singleTask" and SecondActivity.

First, the user launches FirstActivity, and after that SecondActivity starts by clicking a button. From SecondActivity we create a notification in the status bar.

Intent intent = new Intent(SecondActivity.this, FirstActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(FirstActivity.ARG, true); NotificationCompat.Builder builder = new NotificationCompat.Builder(SecondActivity.this); builder.setSmallIcon(R.drawable.ic_launcher) .setAutoCancel(true) .setContentTitle("title") .setContentText("message"); PendingIntent notifyIntent = PendingIntent.getActivity(SecondActivity.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(notifyIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1000, builder.build()); 

When I clicked on this notification and my application is already running, the FirstActivity # onNewIntent method will be executed, and the intent is ARG extra = true. It's good.

When I clicked on this notification and my application is not running, the FirstActivity # onNewIntent method will NOT execute, but instead, the first FirstActivity # onCreate () function will execute, and I can get my ARG flag as getIntent().getBooleanExtra(ARG, false) This intent contains ARG extra = true. This is also the right situation.

But when I try to exit my application after clicking the โ€œSituation 2โ€ button and start the application again, I got an intention in FirstActivity # onCreate () with ARG = true (the old extra value that I processed). Why is my flag โ€œtrueโ€ after reopening the application?

+6
source share
1 answer

I came across this question looking for a solution to a similar problem. Despite the fact that this question is already a year ago, I will write how I solved it if it can help someone else, because it took me many hours to solve my problem. I also have activity declared using android: launchMode as "singleTask". A notification is sent from the service, and this notification has a PendingIntent with the intention to open MyActivity.

My solution is to set the FLAG_ACTIVITY_NEW_TASK flag ( http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK ) in the intent. Then create a pending intent with id 0 and PendingIntent.FLAG_CANCEL_CURRENT.

 Intent _intent = new Intent(context, MyActivity.class); _intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); _intent.putExtra("DATA", myData); PendingIntent _pIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

Each time you click on the notification, an instance of MyActivity is created in the new task and onCreate is called. The intent contains the correct additional data. When I click the back button and a new notification appears, the goal always contains the latest additional data. If the action instance already exists (for example, when I click the "Home screen" button), the onNewIntent method is called and the goal is to fix the new additional data.

This is clearly explained in http://developer.android.com/guide/topics/manifest/activity-element.html#lmode . I also tried using different request codes (arg 2) for each notification in PendingIntent.getActivity and with PendingIntent.FLAG_UPDATE_CURRENT, but I think the secret is in launchMode definition as singleTask and Intent.FLAG_ACTIVITY_NEW_TASK.

I also have another case where launchMode is "standard". Here, activity also begins by clicking on a notification sent from the service. In order for the initial activity to receive the correct additional data, I set the FLAG_ACTIVITY_SINGLE_TOP flag.

 _intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 

This flag allows you to call the onNewIntent method when an activity instance already exists (for example, when I clicked the "Home screen" button). Again with the correct additional data. Otherwise, onCreate is also called with the correct additional data (for example, when I clicked the "Back" button).

An intention pending is created with a new request code each time, but it should also work with the same request code. I think the secret is in launchMode and the intent flag set.

 PendingIntent pIntent = PendingIntent.getActivity(this, notificationRequestCode, _intent, PendingIntent.FLAG_CANCEL_CURRENT); 
+6
source

Source: https://habr.com/ru/post/970802/


All Articles