I have issue in intent of my launcher activity.Scenerio is: 1. Send intents form notification service to my launcher activity
PendingIntent contentIntent = PendingIntent.getActivity(this, TripLoggerConstants.PENDING_TRIPS_NOTIFICATION_ID, new Intent(this, MainActivity.class).putExtra("is_log", true), Intent.FLAG_ACTIVITY_CLEAR_TOP);
2. In my MainActivity i getting this intent. code is:
if(this.getIntent().getExtras()!=null){
boolean isLogNewTripScreen = (boolean)this.getIntent().getExtras().getBoolean("is_log");
}
}
3. this work fine but when i come from notification service,but when i launch from not notification service ,that data in intentis still there.How can i remove that data from intent.
EDIT: I've created a sample application to test this problem and possible solutions. Here are my findings:
If you launch your app from a notification with extras and then later return to your app by selecting it from the list of recent tasks, Android will launch the app again the same way it was launched from the notification (ie: with the extras). This is either a bug or a feature, depending on who you ask.
You'll need to add additional code to deal with this situation. I can offer 2 suggestions:
1. Use
FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
When you create your notification, set the flag
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
in theIntent
. Then, when the user selects the notification and launches the app from the notification, this will not create an entry for this task in the list of recent tasks. Also, if there was an entry in the list of recent tasks for this application, that entry will also be removed. In this case, it will not be possible for the user to return to this task from the list of recent tasks. This solves your problem by removing the possibility that the user launches the app from the list of recent tasks (but only when the app has been launched from the notification).2. Detect
FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
When the user launches your app from the list of recent tasks, Android sets the flag
Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
in theIntent
that is passed toonCreate()
of your launch activity. You can detect the presence of this flag inonCreate()
and then you know that the app has been launched from the recent tasks list and not from the notification. In this case, you can just ignore the fact that the extras in theIntent
still contain data.Choose the solution that best suits the workflow for your application. And thanks for the question, this was an interesting challenge to solve :-)
Additional information:
You are creating the
PendingIntent
incorrectly. You are callingYou are passing
Intent.FLAG_ACTIVITY_CLEAR_TOP
as the 4th parameter togetActivity()
. However, that parameter should bePendingIntent
flags. If you want to setFLAG_ACTIVITY_CLEAR_TOP
on theIntent
, you need to do it this way:I noticed that using fragments. I read a QR Code in Activity A that opens fragment 1, send its content to a webservice and if goes right, replace it with fragment 2. When user press back, the onBackPressed in Activity A call finish. If user select the app again in the list, it was opening fragment 1 instead of fragment 2.
I solved that checking in onBackPressed if extra contains a field indicating that fragment 2 was already opened. If true, moveTaskToBack(true) is called instead of finish()
Activity A
Fragment 2