简体   繁体   中英

Android:Notification starts new activity instead of resuming old one if originally activity is not started from launcher

I have spent a week to find a solution but with no success, so I need a help.

When my application goes to background then notification appears and displays status of activity.

It works perfectly if the application is started from launcher (with action = android.intent.action.MAIN and category = android.intent.category.LAUNCHER ). But if the application is started fe from the gallery using action android.intent.action.SEND or android.intent.action.SEND_MULTIPLE and category android.intent.category.DEFAULT , then notification starts new activity instead of resuming existing one.

The code for notification:

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);

int icon = R.drawable.icon;
Notification notification = new Notification(icon, null, 0);
notification.flags |= Notification.FLAG_ONGOING_EVENT|Notification.FLAG_AUTO_CANCEL|Notification.FLAG_ONLY_ALERT_ONCE;

Context context = this.getApplicationContext();
CharSequence contentTitle = "XXX";
CharSequence contentText = "XXX";

Intent notificationIntent = new Intent(context, MyClass.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent activityIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, activityIntent);

notificationManager.notify(NOTIFICATION_ID, notification);

In other words, I have an application "My Application" with activity "My Activity". If "My Activity" is started on the top of gallery and creates notification, then after pressing the notification "My application" is started instead of resuming the gallery with "My activity".

Do you have any idea how to heal it?

    Intent myIntent = new Intent(this, MainActivity.class);
            myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

            PendingIntent pendingIntent = PendingIntent.getActivity(
                    getBaseContext(), 0, myIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT);


NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notification.flags = notification.flags
                | Notification.FLAG_ONGOING_EVENT;
        notification.flags |= Notification.FLAG_NO_CLEAR;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM