4

I have a service with an ongoing notification. in the app itself I have two activities - HomeActivity and SettingsActivity.

Currently what I have is -> when the notification is clicked:

  1. If the app is closed -> open the HomeActivity.

  2. If the current showing activity is HomeActivity, bring it to front without creating a new one.

Code:

resultIntent = new Intent(context, HomeActivity.class);
resultPendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = mBuilder
                .setSmallIcon(notificationData.getImageSrc())  // the status icon
                .setTicker("HealthChecker")  // the status text
                .setWhen(System.currentTimeMillis())  // the time stamp
                .setContentTitle("HealthChecker")  // the label of the entry
                .setContentText(notificationData.getText())  // the contents of the entry
                .setOngoing(true).setContentIntent(resultPendingIntent)
                .build();

Manifest: I added `android:launchMode="singleTop":

<activity
            android:name=".ui.HomeActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:theme="@style/AppTheme.NoActionBar"></activity>

The problem I have is:

when I am in the SettingsActivity and I click the notification, it opens a new instance of HomeActivity.

What I want is when the app is opened in either activity and notification is clicked -> show current activity, and if app is closed and notification is clicked, open new instance of HomeActivity.

Ofek Agmon
  • 5,040
  • 14
  • 57
  • 101
  • i think you should read this https://developer.android.com/training/notify-user/navigation.html – Raghunandan May 31 '16 at 15:33
  • 1
    I closed this as duplicate. Please see http://stackoverflow.com/questions/5502427/resume-application-and-stack-from-notification as it covers exactly this problem. – David Wasser Jun 01 '16 at 17:34

1 Answers1

3

when I am in the SettingsActivity and I click the notification, it opens a new instance of HomeActivity.

Because this is what singleTop is for:

However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created. In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.

which is your case because what you have on top is SettingsActivity.

What I want is when the app is opened in either activity and notification is clicked -> show current activity, and if app is closed and notification is clicked, open new instance of HomeActivity

Then your notification should perhaps send intent to 3rd activity (w/o ui) which would then redirect further and finish(). Depending on what is your targetSdk you may want to use ActivityLifecycleCallbacks to figure out when your app is front most one or not.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Although this answer will work, it is the very long way around. You end up writing a lot of unecessary code and complicating your app. The problem OP has is very common and there is a very easy way to deal with it. Please see http://stackoverflow.com/questions/5502427/resume-application-and-stack-from-notification – David Wasser Jun 01 '16 at 17:35
  • @DavidWasser that's should work and if so, it's better solution than mine, but if you follow the comments bellow the accepted answer there then it sounds like not a rock solid solution. – Marcin Orlowski Jun 01 '16 at 17:47
  • Yes it does work. All the time. You can safely ignore the few comments from people who say that it doesn't. I think they probably had other problems that were causing them issues. – David Wasser Jun 01 '16 at 17:54
  • @DavidWasser great solution! thanks! works great – Ofek Agmon Jun 02 '16 at 08:05
  • @DavidWasser actually, after further testing, it doesn't work in one scenario: launching the app, (notification not running), activity A is on front, starting the notification, (notification running), going to activity B, clicking the notification opens activity A again. it works great, but only after starting notification and closing the app (removing from opened app list), it works as excepted afterwords for each scenario. – Ofek Agmon Jun 02 '16 at 08:16
  • anyone knows how to handle this issue? – Ofek Agmon Jun 02 '16 at 08:17
  • @OfekAgmon please post a new question with this problem. Explain exactly what you are doing (post code) and explain what you are seeing. I'm sure that there is a reasonable explanation. This comment thread in this answer in this closed question is not the right place to explore your problem. – David Wasser Jun 02 '16 at 09:37