5

I have an activity with launchMode set to singleTask:

<activity
    android:name="com.blah.blah.MyActivity"
    android:launchMode="singleTask">
</activity>

I have an ongoing notification with a PendingIntent that launches that activity:

Intent activityIntent = new Intent( this,
                                    MyActivity.class );
TaskStackBuilder stackBuilder = TaskStackBuilder.create( this );
stackBuilder.addParentStack( MyActivity.class );
stackBuilder.addNextIntent( activityIntent );
PendingIntent resultingActivityPendingIntent = stackBuilder.getPendingIntent( REQUEST_CODE,
                                                                              PendingIntent.FLAG_UPDATE_CURRENT );
...
m_notificationBuilder.setContentIntent( resultingActivityPendingIntent );
startForeground( ONGOING_NOTIFICATION_ID,
                 m_notificationBuilder.build() );

When I am interacting with an existing MyActivity, then I hit Home and restart MyActivity via the launcher, MyActivity's onNewIntent() gets called as expected.

The problem is that when I am interacting with an existing MyActivity, and I click on the ongoing notification, a new MyActivity is created via onCreate(), and the existing one is destroyed via onDestroy(). I expected that MyActivity's onNewIntent() would be called instead. Why doesn't this happen?

I have tried these answers without success:

Community
  • 1
  • 1
Kevin
  • 702
  • 7
  • 22

1 Answers1

10

Your problem is due to the use of TaskStackBuilder. This code is causing your problem:

Intent activityIntent = new Intent( this,
                                MyActivity.class );
TaskStackBuilder stackBuilder = TaskStackBuilder.create( this );
stackBuilder.addParentStack( MyActivity.class );
stackBuilder.addNextIntent( activityIntent );
PendingIntent resultingActivityPendingIntent =
         stackBuilder.getPendingIntent(REQUEST_CODE,
         PendingIntent.FLAG_UPDATE_CURRENT );

When you use TaskStackBuilder this way, it sets additional flags in the generated Intents that cause the task to be reset (all Activities in the task are destroyed) before your Activity gets launched.

Instead use:

Intent activityIntent = new Intent( this,
                                MyActivity.class );
PendingIntent resultingActivityPendingIntent =
         PendingIntent.getActivity(this, REQUEST_CODE,
             activityIntent, PendingIntent.FLAG_UPDATE_CURRENT );
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 1
    This solution works for me, but for some reason I had to completely uninstall my app first before it worked. If I simply make the change, rebuild, then run (per usual), the old, incorrect behavior happens. In fact, now that it's working correctly, if I switch back to using the TaskStackBuilder without uninstalling my app, the correct behavior still occurs! To be sure, I can make a different change in the same class, and it will be picked up. It's quite bizarre. I will try to characterize it further and post a separate question, if necessary. Thanks. – Kevin Feb 06 '15 at 08:05
  • 2
    this is the best answer..spent almost 2 weeks before I found this. However as Kevin mentioned, you have to uninstall the app and then try it – Dinesh Beura Apr 11 '22 at 17:29