I'm having trouble using TaskStack for notifications. I am trying to keep the navigation flow of my application while listening to a notification.
The navigation style of my application is similar:
PrincipalActivity (parent) -> SecondaryActivity (child)
Sometimes the GCM service may trigger notifications starting with SecondaryActivity . A PrincipalActivity instance may or may not exist, so I am implementing a custom PendingIntent with TaskStack (see below). This works when PrincipalActivity paused or closed.
The problem occurs when the user clicks on the notification, and PrincipalActivity in the foreground: PendingIntent calls PrincipalActivity.onDestroy() and creates a new action stack that loses all the data.
The service calls a notification:
Intent notificationIntent = new Intent(context, SecondaryActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addParentStack(SecondaryActivity.class); stackBuilder.addNextIntent(notificationIntent); PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); mNotifyBuilder.setContentIntent(pendingIntent); mNotificationManager.notify(id, mNotifyBuilder.build());
AndroidManifest.xml
<activity android:name="com.hitsbam.geotalk.Principal" android:configChanges="orientation|screenSize|keyboardHidden" android:label="@string/principal_titulo" android:launchMode="singleTask" /> <activity android:name="com.hitsbam.geotalk.Secondary" android:configChanges="orientation|screenSize|keyboardHidden" android:label="@string/peticiones_titulo" android:parentActivityName=".Principal" />
I tried with the PendingIntent flags ( FLAG_UPDATE_CURRENT , FLAG_ONE_SHOT ) and experimented with various launch_mode for manifest actions ( singleTask , singleTop , singleInstance ), but no luck.
How can I create the above custom TaskStack with activity and parents if this parent is already in the foreground and has not lost data? (Ideally, reusing activity on the stack?)