简体   繁体   中英

android preserve activity flow on startactivity

I'm having following activity flow in normal case:

在此处输入图片说明

Activity B can be started by any activity. When B is started from any activity, on returning from B, it returns to the activity that started the activity B, whereas I want to preserve normal flow and go back to A instead of the activity that started it.

Manifest has:

<activity
    android:name=".Bactivity"
    android:label="@string/title_b"
    android:theme="@style/AppTheme"
    android:parentActivityName=".Aactivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.mycompany.myfirstapp.Aactivity" />
</activity>

I start activity B from any other activity using:

Intent intent = new Intent(getApplicationContext(), Bactivity.class);

intent.putExtra(SELECTED_CONTACT_ID, contact_id);
intent.putExtra(SELECTED_CONTACT_NAME, contact_name);

startActivityForResult(intent, 1);

Also, if started from a notification, I do:

Intent intent = new Intent(this, Aactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
        PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setContentIntent(pendingIntent).......;

How can I return from activity B to activity A even if B is started by any activity or notification?

You could use pending intents when trying to start Activity B, in order to add its parent activities to the back stack, for example (as you've already done in your code):

<activity
    android:name=".Bactivity"
    android:label="@string/title_b"
    android:theme="@style/AppTheme"
    android:parentActivityName=".Aactivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.mycompany.myfirstapp.Aactivity" />
</activity>

And then use TaskStackBuilder when creating the notification, so that a proper back stack gets set up:

// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this,Bactivity.class);

// Intent for the activity to go to back stack
Intent upIntent = new Intent(this,Aactivity.class);

// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
    TaskStackBuilder.create(this)
                    // add all of DetailsActivity's parents to the stack,
                    // followed by DetailsActivity itself
                    .addNextIntentWithParentStack(upIntent)
                    .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);

Moreover, to obtain an up navigation when starting from another activity rather than from a notif, use NavUtils (given you have specified the parent activity in the manifest as before):

In Bactivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    getActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

This way you'll get the desired behavior. You can add complexity to the flow as much as you want, just check implementing back navigation and providing proper back navigation .

Cheers!

PLUS

Remember that if you want to add extras to the intent you have to add also an action, otherwise they won't get seen by the activity that is started with extras from a notification.

// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this,Bactivity.class);
detailsIntent .putExtra(SELECTED_CONTACT_ID, contact_id);
detailsIntent .putExtra(SELECTED_CONTACT_NAME, contact_name);
detailsIntent .setAction(Long.toString(System.currentTimeMillis())); // Set a unique id (dummy) action

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM