Skip to content Skip to sidebar Skip to footer

How To Make Notification Resume And Not Recreate Activity?

I thought I had this figured out, but after some debugging on this question: How to make notification uncancellable/unremovable I just realized my activity is still getting onCreat

Solution 1:

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

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

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

Replacing them with a regular pendingintent like this solved it:

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

An complete example

Intentintent=newIntent(getApplicationContext(), myactivity.class);
PendingIntentpIntent= PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), intent, 0);

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


NotificationManagernotificationManager=
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, myNotification);

Solution 2:

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

Solution 3:

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)

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

Now it's ok, changed to this code:

Intentintent= getActivity().getIntent();
PendingIntentpendingIntent=  PendingIntent.getActivity(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Hope this help ^_^

Solution 4:

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.

Solution 5:

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.

Post a Comment for "How To Make Notification Resume And Not Recreate Activity?"