Intent with old extra in onCreate() for singleTask Activity

戏子无情 提交于 2019-12-01 17:58:42

I came across this question while looking for a solution to a similar problem. Even though this question is from an year ago, I’ll write down how I solved it in case it can help somebody else because it took me many many hours to solve my problem. I also have the activity declared with the android:launchMode as “singleTask”. A notification is sent from a service and this notification has a PendingIntent with an Intent to open MyActivity.

My solution is to set the flag FLAG_ACTIVITY_NEW_TASK (http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK) in the intent. Then create the 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);

Whenever the notification is clicked an instance of MyActivity is created in a new task and onCreate is called. The Intent contains the correct extra data. When I press the back button and there is a new notification, the intent always brings the newest extra data. If an instance of the activity already exists (for example when I press the Home Screen button), onNewIntent method is called and the intent brings the correct new extra 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) per notification in PendingIntent.getActivity and with PendingIntent.FLAG_UPDATE_CURRENT but I think the secret is in the definition of launchMode as singleTask and Intent.FLAG_ACTIVITY_NEW_TASK.

I also have another case in which the launchMode is "standard". Here the activity is also started by clicking a notification sent from a service. In order to have the started activity receive the correct extra data I set the flag FLAG_ACTIVITY_SINGLE_TOP.

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

This flag makes the method onNewIntent to be called when an instance of the activity already exists (When I pressed Home Screen button for example). Again with the correct extra data. Otherwise onCreate is called also with the correct extra data (When I pressed the back button for example).

The Pending intent is created with a new request code every time but it should work also with the same request code. I think the secret is in the launchMode and the intent flag set.

PendingIntent pIntent = PendingIntent.getActivity(this, notificationRequestCode, _intent, PendingIntent.FLAG_CANCEL_CURRENT);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!