Reputation: 17375
I'm creating a notification from a service with the following code:
NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence tickerText = "bla ...";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon,
tickerText, when);
Intent notificationIntent = new Intent(ctx, SearchActivity.class).
putExtra(SearchActivity.INTENT_SOURCE,
MyNotificationService.class.getSimpleName());
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
notificationIntent, 0);
notification.setLatestEventInfo(ctx, ctx.getString(R.string.app_name),
tickerText, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1, notification);
The logs clearly says that the the method startActivity is called twice times:
04-02 23:48:06.923: INFO/ActivityManager(2466): Starting activity: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,520][480,616] (has extras) }
04-02 23:48:06.923: WARN/ActivityManager(2466): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,520][480,616] (has extras) }
04-02 23:48:06.958: INFO/ActivityManager(2466): Starting activity: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,0][480,96] (has extras) }
04-02 23:48:06.958: WARN/ActivityManager(2466): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.SEARCH cmp=com.xy/.SearchActivity bnds=[0,0][480,96] (has extras) }
04-02 23:48:07.087: INFO/notification(5028): onStartCmd: received start id 2: Intent { cmp=com.xy/.NotificationService }
04-02 23:48:07.310: INFO/notification(5028): onStartCmd: received start id 3: Intent { cmp=com.xy/.NotificationService }
04-02 23:48:07.392: INFO/ActivityManager(2466): Displayed activity com.xy/.SearchActivity: 462 ms (total 462 ms)
04-02 23:48:07.392: INFO/ActivityManager(2466): Displayed activity com.xy/.SearchActivity: 318 ms (total 318 ms)
Why are they started twice?
There are two identical questions on stackoverflow: here and here. But they do not explain what the initial issue could be and they do not work for me. E.g. changing to launchMode singleTop is not appropriated for me and it should work without changing launchMode according to the official docs (see Invoking the search dialog).
Nevertheless I also tried to add the following flags to notificationIntent
Intent.FLAG_ACTIVITY_CLEAR_TOP | PendingIntent.FLAG_UPDATE_CURRENT
but the problem remains the same.
Upvotes: 11
Views: 12834
Reputation: 51
Please use the flag like the following.
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
|Intent.FLAG_ACTIVITY_SINGLE_TOP);
Upvotes: 5
Reputation: 53
Call it on key released or key pressed (avoid general ACTION)!
if(event.getAction() == event.ACTION_UP){
Intent intent = new Intent(......);
startActivityForResult(intent, 0);
}
Upvotes: 1
Reputation: 2260
Same here, problem occurs on Samsung Galaxy S. Any good idea for a workaround?
I am now checking if a similar Intent was already received and finish the activity if so. It works but it doesn't look very nice.
Can we somehow cancel the second intent?
UPDATE: I could prevent the problem by setting the FLAG_ONE_SHOT like this:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Upvotes: 16
Reputation: 17375
This seems to be a samsung galaxy bug. Confirmed that all is ok on a different device.
(See also no discussion at google groups)
One workaround can be to cancel the second intent with the awkward but working trick (not sure if this has side effects on other devices):
// put a hint into the notification and check if the intent
// comes from notification or not:
String intentSource = queryIntent.getStringExtra(INTENT_SOURCE);
if (NotificationService.class.getSimpleName().equals(intentSource)) {
// don't do this again e.g. when rotating!
queryIntent.removeExtra(INTENT_SOURCE);
if (getIntent() != null && getIntent().getSourceBounds() != null) {
if (getIntent().getSourceBounds().top == 0
&& getIntent().getSourceBounds().left == 0) {
Log.w("search", "Problematic double trigger!");
}
}
}
So use this with care and probably check if a previous click was done in the last second then it is very likely that it is the double-trigger problem.
Upvotes: 0