2

Seems that onNewIntent() of my MainActivity is not being called when I have another Activity on top of it, if I close the second Activity it does get called just fine, it's like the other Activity which is on top is blocking the messages? Both Activitys are set to singleTop in the manifest and I set the Intent from a notification:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

Does anybody know what could be wrong?

Thanks!

Willis
  • 5,308
  • 3
  • 32
  • 61
eXistenZ
  • 326
  • 4
  • 14
  • 2
    If another `Activity` is visible over an existing `Activity` the the first will be paused and then stopped. Consequently no code in the first `Activity` will be executed. This is standard for Android and the `Activity` life-cycle. – Squonk Apr 20 '15 at 21:12
  • So if I wanted to call a function when the notification is clicked and there are other activities on top, how would I do that without creating a new activity? – eXistenZ Apr 20 '15 at 21:17

2 Answers2

2

You have probably already searched google - there are lots of questions on why onNewIntent is not called.

It might depend on which android version you are doing tests, but you might try adding Intent.FLAG_ACTIVITY_SINGLE_TOP flag to your intent - even though you have it in your manifest, together with Intent.FLAG_ACTIVITY_CLEAR_TOP.

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

this will remove activities above you activity which you want to launch.

But if your intent is to keep two activities alive, and switch between them, then look into android:taskAffinity and FLAG_ACTIVITY_NEW_TASK.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • I tried adding Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP but it did not close the activity which is on top, it doesn't do anything. Would be fine for me to close any activities which are on top of the main one. Any ideas why it did not work? – eXistenZ Apr 20 '15 at 21:38
  • Actually it worked fine, not sure why it didn't first time, thanks! – eXistenZ Apr 20 '15 at 22:01
  • Actually, it doesn't seem to work all the time, weird - if I press the home button and then I click on the notification, it won't clear the activity on top of it anymore, it just brings it to view. – eXistenZ Apr 20 '15 at 22:13
  • see this so: http://stackoverflow.com/questions/24873131/android-clear-task-flag-not-working-for-pendingintent – marcinj Apr 20 '15 at 22:54
  • I think it's fine now, I had some code on my onCreate() to fix some other bug caused by the android installer launching my application in different way, changed it to this and seems fine now: `/* if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { */ if (!isTaskRoot()) { finish(); return; }` Thanks! – eXistenZ Apr 21 '15 at 00:39
1

Verify that the correct launch mode is set for the Activity receiving the Intent. Set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent).

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • It is set to singleTop but looks like as Squonk pointed out, when there is another activity on top of it, the activity beneath won't execute any code. – eXistenZ Apr 20 '15 at 21:25