3

I've been doing a lot of research on this and I've hit a wall. I've read what seems like all of the material on this topic (inluding this, this, and this as well as the docs) and nothing is helping me in what I want to do.

Basically, in the case that the app is open on the user's phone, I just want a notification to redirect the user to the already-existing Activity in my app. I use a pattern like this:

private PendingIntent buildPendingIntentForNotification(String type) {
        Intent resultIntent = new Intent(this, MainActivity.class);
        resultIntent.setAction(Intent.ACTION_VIEW);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);// have tried without this and with Intent.FLAG_ACTIVITY_CLEAR_TOP
        resultIntent.putExtra(CommonUtils.NOTIFICATION_TYPE, type);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        return resultPendingIntent;
    }

I've also tried declaring all 4 types of launchModes in the Android Manifest within the activity tag. No matter what I do, clicking on the notification (even if the app is the active foreground app on the screen and MainActivity is the active activity) always seems to restart the activity from onCreate. From what I can see during debugging, onDestroy is not called at any time between getting the notification and clicking on it. However, after the notification is clicked, onCreate is called and THEN onDestroy is called even though my activity is not being destroyed, which is very strange. I'm hoping someone can help me make sense of this because all of the launchMode and Intent.setFlags suggestions are not working for me. Thanks a lot!

Community
  • 1
  • 1
ethan123
  • 1,084
  • 2
  • 14
  • 26

3 Answers3

3

Just for info's sake, here's the code I used to fix my problem (with credit to David's solution):

private PendingIntent buildPendingIntentForNotification(String type) {
        Intent resultIntent = new Intent(this, MainActivity.class);
        //resultIntent.setAction(Intent.ACTION_VIEW);
        resultIntent.setAction(Intent.ACTION_MAIN);
        resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resultIntent.putExtra(CommonUtils.NOTIFICATION_TYPE, type);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        return resultPendingIntent;
    }
ethan123
  • 1,084
  • 2
  • 14
  • 26
0

There are 2 ways of doing this:

  1. Notification to restore a task rather than a specific activity?

  2. Resume application and stack from notification

However, you may also be seeing this nasty Android bug.

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
-1

You cannot control and cannot be sure about the state of your activities on the Android, meaning (in your context) you can only start an activity that has been paused not one that has been destroyed. The OS decides which activities will keep paused and which will destroy and you have no control over that. When your activity leaves the foreground successive callbacks are being invoked at undefined times and to the OS's

What you can simply do is to save your activity's instance state in the onPause() method (which we know for sure that will be called as soon as the activity leaves the foreground) and the on onCreate() you can restore the activity with the data as it previously was.

Note: If you have for example Activity A and activity B and you start activity B from within activity A, then on activity B's onCreate() method you can getIntent().getExtras()

George Daramouskas
  • 3,720
  • 3
  • 22
  • 51
  • This answer is nonsense. There are very simple ways to just bring your task to the foreground, no matter what state it is in. You don't need to know what state it is in to do this. – David Wasser Feb 12 '15 at 15:13