How to make notification resume and not recreate activity?

我的未来我决定 提交于 2019-11-28 23:04:32
CeeRo

Looks like the problem was caused by these lines in the notification-code (taken straight from Android's guide on notifications:

TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(FieldAgent.class);
    stackBuilder.addNextIntent(intent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,  PendingIntent.FLAG_UPDATE_CURRENT);

Replacing them with a regular pendingintent like this solved it:

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

An complete example

Intent intent = new Intent(getApplicationContext(), myactivity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), intent, 0);

Notification myNotification  = new Notification.Builder(getApplicationContext())
                        .setContentTitle("Title")
                        .setContentText("Some text....")
                        .setSmallIcon(R.drawable.myicon)
                        .setContentIntent(pIntent)
                        .setAutoCancel(true).build();


NotificationManager notificationManager =
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, myNotification);

In my case, the answer have by CeeRo worked but in a weird fashion. I HAD TO RESTART THE PHONE! :)

Hung

I don't know if my case like you or not. I tried to create intent as you. I am using pageviewer + fragment. My case, click notification message to go app then home to main, then start app again, activity recreated (this code in class fragment)

Intent intent = new Intent(getActivity(),MainActivity.class);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent =  PendingIntent.getActivity(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Now it's ok, changed to this code:

Intent intent = getActivity().getIntent();
PendingIntent pendingIntent =  PendingIntent.getActivity(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Hope this help ^_^

Have you overriden onBackPressed on your Activity? I think if you don't override onBackPressed, Android's default is to destroy (finish()) the Activity. So probably if you are going back then your notification checks whether the Activity already exists, finds none and then creates a new one.

You have a lot of code that is being run multiple times.

Remove:

saveVariables();
setVisible(false);

From onStop() and onDestroy(). This code will always have been run first already in onPause().

loadVariables(); may also be ran twice. Once in onCreate() and once in onResume().

It may be wise to change your loading logic somewhat.

It may be possible that your init() code is being ran twice. Once in onCreate() and once in onActivityResult().

TL;DR As for your actual problem:

onActivityResult() is a bit weird sometimes, in that it doesn't always fire when you think it should. Try loading your variables before the init() in onActivityResult().

To experiment with this, place a log in your onCreate(), onStart(), onResume() and onActivityResult() and notice when each is started.

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