1

Before asking this question, I did a lot of search. But none of the answers here did work for me:
onNewIntent is not called,
onNewIntent not called on restart,
onNewIntent() not getting called in some devices,
my onNewIntent is not calling

I am working on NotificationManager where my requirement is to open an Activity on tap of notification with values received through that notification.
I have tried the following piece of code while setting intent:

Intent notificationIntent = new Intent(context, MyActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

I also set launchMode as singleTop in Manifest.xml:

<activity android:name=".MyActivity" android:hardwareAccelerated="true" android:launchMode="singleTop">
    <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:host="www.google.com"
                android:scheme="http" />
    </intent-filter>

</activity>

The issue seems to be device specific. I tried the same piece of code on Nexus 7 and onNewIntent is being called there. But somehow it is not working on Galaxy S4.

Questions:

  1. Have you guys faced similar problem in the past?
  2. How do I fix it?
  3. My biggest curiosity lies on "How can an API be device specific?"
  4. Can I rely on onNewIntent API or should I look for an alternative? As observed, it seems like this issue doesn't repro on all devices. And I cannot test on all devices for its reliability.
Community
  • 1
  • 1
Green goblin
  • 9,898
  • 13
  • 71
  • 100
  • Is `MyActivity` active (not finished) and in the task's back stack already when the notification is opened? Are you absolutely sure? – David Wasser Jan 23 '15 at 10:37
  • 1
    If `MyActivity` is not in the back stack, Android will create an instance of `MyActivity` and call `onCreate()`, but it **will not** call `onNewIntent()`. That's why it matters ;-) – David Wasser Jan 23 '15 at 12:55
  • I got your point. But how is it that the same scenario works in Nexus 7 and fails in S4? Ah, is it due to fragmentation? – Green goblin Jan 23 '15 at 18:40
  • I'm not sure yet. I'm trying to gather information at this stage. If I knew the answer to your question I would have already told you. – David Wasser Jan 23 '15 at 22:02
  • As of now I have figured out some alternative solution, a middle man `Activity` serves the purpose. But, I would appreciate if you find something and share with me. – Green goblin Jan 24 '15 at 13:25
  • Please explain how you've worked around this. I'm curious. – David Wasser Jan 26 '15 at 08:50
  • My problem was that I wanted to pass some data to a `LauncherActivity` on click of a notification. `onNewIntent` was such API where I was setting the new Intent and starting `LauncherActivity`. Since, onNewIntent was not getting called, `LauncherActivity` was being started with default intent and not with the intent it should. I got rid of `onNewIntent` completely by introducing a `MiddleActivity` and setting this activity to launch on click of notification. From this `Activity`, I launched the desired `LauncherActivity` by getting and setting correct `Intent` – Green goblin Jan 26 '15 at 08:58

0 Answers0