Intent to resume a previously paused activity (Called from a Notification)

扶醉桌前 提交于 2019-12-18 11:05:04

问题


I'm developing an app which shows a notification to the user. The notification's objective is to make it easy to the user to return to the activity when the user is in another activity. I am using this code in my app to create and show the notification.

                    notification = new Notification(R.drawable.icon,
                            "Notify",
                            System.currentTimeMillis());
                    notification.setLatestEventInfo(this, "App name",
                            "App message",
                            PendingIntent.getActivity(
                                    this, 0,
                                    new Intent(this, Main.class),
                                    PendingIntent.FLAG_CANCEL_CURRENT));
                    notification.flags |= Notification.FLAG_ONGOING_EVENT;
                    nManager.notify(0, notification);

But when the user taps the notification starts a new instance of the same activity, instead of the one that the user was using before.

I think this has something to do with PendingIntent but I can't find how to make that Intent to resume a previously paused instance of the activity instead of creating a new instance.

Thanks.


回答1:


I found out how to do it. I added the following code:

notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Now my code looks like this:

    notification = new Notification(R.drawable.icon,
            "Notify", System.currentTimeMillis());
    notification.setLatestEventInfo(this, "App name",
            "App message", PendingIntent.getActivity(this,
                    0, new Intent(this, Main.class)
                            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                                    | Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    PendingIntent.FLAG_CANCEL_CURRENT));
    notification.flags |= Notification.FLAG_ONGOING_EVENT;



回答2:


This helps for me android:launchMode="singleInstance"

<activity
        android:name="com.mosis.automatskidnevnik.MainActivity"
        android:launchMode="singleInstance"
        android:label="@string/app_name" >
        ...



回答3:


I solved the same problem by setting

android:launchMode="singleTask"



回答4:


When creating your PendingIntent, you use:

Main.this.getBaseContext()

You should use the default Activity context if you want to return to the same activity. Read more here:

Android - what's the difference between the various methods to get a Context?




回答5:


Apart from the correct answer of @Jimix I would change the PendingIntent requestCode from 0 to a non zero value as commented on another answer. On the other hand, there is a known bug on previous Android versions that took me a couple of desperate hours. The solution proposed there worked fine for me cancelling the PendingIntent before of sending the notification:

if (Build.VERSION.SDK_INT == 19) {
   getNotificationPendingIntent().cancel();
}


来源:https://stackoverflow.com/questions/5247902/intent-to-resume-a-previously-paused-activity-called-from-a-notification

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