Question

I'm developing an android application, and in one activity, when the user presses the HOME button, a notification appears. Upon pressing the notification the activity would resume from where it left off. The problem is that the activity is recreated each time. Upon pressing the notification onDestroy() and then onCreate() is called. But say if I reopen the activity through the "Recent apps" it resumes successfully.

This question has been asked before on stackoverflow and I have tried the answers provided and none of them seem to work. The API that I'm using is the latest 4.4. The options I have tried are:

  • Playing around with the Intent flags

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

  • Setting the activity to singleInstance and singleTask

How can I mimic the intent that is sent when I open my activity through 'Recent apps'?

EDIT: One more caveat that might be important. There's 3 activities in my program A B C. When clicking on the launcher icon activity A is first started. The notification is created only when going from activity C to Home. I'm trying to resume activity C.

Was it helpful?

Solution

Problem solved.

I was debugging the application through LogCat and looking at the intents that were being generated by the ActivityManager. Whenever I was changing the flags of the Intent, I wouldn't see the change in LogCat. So

02-12 11:26:39.586: I/ActivityManager(766): START u0 {flg=0x1000c000 cmp=com.example.app/.controllers.Activity3 bnds=[0,153][1080,345]} from pid -1

Changing the Intent flags didn't change the flg variable in the log. Apart from this, the notification image was staying in the Status bar, my app wasn't destroying it. What I did was add the PendingIntent.FLAG_CANCEL_CURRENT flag to the PendingIntent I was sending:

    Intent intent = new Intent(this, Activity3.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            intent, PendingIntent.FLAG_CANCEL_CURRENT);
    mBuilder.setContentIntent(pendingIntent);

So the problem was an incorrect PendingIntent that I must have created before, that was never being removed. Now I'm able to resume the activity by clicking the notification. Hope this helps someone that might have a similar issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top