android保留活动开始活动

时间:2015-10-11 15:08:38

标签: android android-intent android-activity

我在正常情况下有以下活动流程:

enter image description here

活动B可以由任何活动启动。当B从任何活动开始时,从B返回时,它返回到启动活动B的活动,而我想保留正常流量并返回到A而不是启动它的活动。

清单有:

<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>

我使用以下任何其他活动开始活动B:

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

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

startActivityForResult(intent, 1);

另外,如果从通知开始,我会这样做:

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).......;

即使B由任何活动或通知启动,我如何从活动B返回活动A?

1 个答案:

答案 0 :(得分:0)

您可以在尝试启动活动B时使用挂起意图,以便将其父活动添加到后台堆栈中(例如,您已在代码中完成):

<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>

然后在创建通知时使用TaskStackBuilder,以便设置正确的后台堆栈:

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

此外,要从另一个活动开始而不是从一个通知开始获取向上导航,请使用NavUtils(假设您已经像以前一样在清单中指定了父活动):

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);
}

这样您就可以获得所需的行为。您可以根据需要为流量添加复杂性,只需检查implementing back navigationproviding proper back navigation

干杯!

<强> PLUS

请记住,如果您想要为意图添加额外内容,您还必须添加一个操作,否则他们不会被通知中的附加内容启动的活动看到。

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