After receiving a notification in my app, clicking on it opens activity B. Activity B has a parent activity A. Here is the manifest:
If @Manish Mulimani's solution worked for you, then please feel free to ignore this information. It didn't work for me, and I haven't tried the code that is in OP, but here is an alternate solution if neither of those worked for you:
First, as OP did, specify the parent Activity in AndroidManifest.xml:
<activity
android:name=".DetailActivity"
android:label="@string/activity_detail"
android:parentActivityName=".MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Then, in the code you write to show your notification, do the following:
/* The Intent you want to open when your notification is clicked */
Intent detailIntent = new Intent(context, DetailActivity.class);
/* If you have some extras or a URI, set it here for the DetailActivity */
detailIntent.setData(todaysWeatherUri);
/*
* This Intent is the one that goes back to your MainActivity when the up
* arrow or back button is clicked from the DetailActivity.
*/
Intent backToMain = new Intent(context, MainActivity.class);
PendingIntent resultPendingIntent =
TaskStackBuilder.create(context)
.addNextIntentWithParentStack(backToMain)
.addNextIntent(detailIntent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
I've tested this with every iteration of starting Activities that I can thin of and it works perfectly! I hope it helps you.
Here is the link for the Android Documentation from where I formed this solution.
Up to parent activity - on Android
Simply you can start activity parent activity with some boolean argument and if boolean argument is set in activity b then launch activity A. This way both activities will be live on stack or use taskstackbuilder.
While using TaskStackBuilder, Do not forget to add Parent activity declaration in Manifest.
For API 16 + :
<activity android:name=".SecondActivity"
android:parentActivityName=".MainActivity"
... />
For API < 16
<activity android:name=".SecondActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<activity
android:name=".SecondActivity"
android:parentActivityName=".ParentActivity"
android:screenOrientation="portrait"/>
val secondIntent = Intent(this, SecondActivity::class.java)
val resultPendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
addNextIntentWithParentStack(secondIntent)
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
}
The TaskStackBuilder
parentStack solution only works for Android < 7.
For Android 7+ I think you should add both parentActivity and secondActivity to the TaskStack.
val parentIntent = Intent(this, ParentActivity::class.java)
val secondIntent = Intent(this, SecondIntent::class.java)
val pendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
addNextIntent(parentIntent)
addNextIntent(secondIntent)
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
}
You need to set up the PendingIntent
which is used to build Notification
, to start a fresh task, and provide the PendingIntent
with a back stack
to achieve the application's normal Up behavior.
Intent resultIntent = new Intent(this, SecondActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// All the parents of SecondActivity will be added to task stack.
stackBuilder.addParentStack(SecondActivity.class);
// Add a SecondActivity intent to the task stack.
stackBuilder.addNextIntent(resultIntent);
// Obtain a PendingIntent for launching the task constructed by this builder.
PendingIntent pendingIntent = stackBuilder.getPendingIntent(REQUEST_CODE, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("My Notification")
.setContentText("Notification content")
.setSmallIcon(android.R.drawable.ic_menu_view)
.setContentIntent(pendingIntent)
.build();
manager.notify(NOTIFICATION_ID, notification);
Please read the Android official documentation on Preserving Navigation when Starting an Activity. It recommends the above approach.
I use the following code and it works like a charm. Have a go!
Intent upIntent = new Intent(getApplicationContext(), Home.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
Log.d("ShowNotifications", "New Home");
TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
} else {
Log.d("ShowNotifications", "Old Home");
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
//upIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(upIntent);
finish();
}