vlio20 vlio20 - 1 year ago 68
Android Question

Up to parent activity - on Android

After receiving a notification in my app, clicking on it opens activity B. Activity B has a parent activity A. Here is the manifest:

android:screenOrientation="portrait" >

<!-- Parent activity meta-data to support 4.0 and lower -->
android:value="com.evapp.activities.A" />

In activity B I have the up functionality enabled (left arrow near the activity action's bar icon), here is the code:

protected void onCreate(Bundle savedInstanceState)

The problem is that if activity B was opened via clicking on the notification (activity A was not the one to bring activity B) the when clicking on the icon the app is closed. I would like to make it open it's parent activity, A. Is it possible? or should I do it with
from activity B?

Update 1- I have added this code:

public boolean onOptionsItemSelected(MenuItem item)
switch (item.getItemId())
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent))
NavUtils.navigateUpTo(this, upIntent);

return true;


Answer Source

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.
// Add a SecondActivity intent to the task stack.

// 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")

manager.notify(NOTIFICATION_ID, notification);

Please read the Android official documentation on Preserving Navigation when Starting an Activity. It recommends the above approach.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download