0

我有活动流程:

+--+   +--+   +----+
|A1|-->|A2|-->|Home|
+--+   +--+   +----+

A1 和 Home 中的 BACK 按钮应关闭应用程序。所以我finish()在 A2 启动时调用 A1,并且在 Home 启动时调用 A2(感谢 A1 和 A2 永远不会保留在后堆栈中)。

但是,A2 中的 BACK 按钮应该指向 A1,所以我在 A2 中覆盖(A1 已经完成,如上所述,所以我必须重新启动它)

@Override
public void onBackPressed() {
    final Intent intent = new Intent(getApplicationContext(), A1.class);
    startActivity(intent);
    finish();
}

现在,可以使用 Home 中的按钮重新开始序列

Home (button pressed)->A1->A2->(return to Home)

在这种情况下(即在 Home 之后启动 A1->A2 时)A1 的布局应该有点不同。

4

1 回答 1

0

您将需要使用 UP 操作,而不是 BACK 操作。首先阅读此内容以了解区别http://developer.android.com/training/implementing-navigation/ancestral.html

此外,这也是您可以从 Home 跳过 A2 的方法:

显现

<activity
    android:name="com.yourpackage.A1">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

<activity
    android:name="com.yourpackage.A2"  
    android:parentActivityName="com.yourpackage.A1" >

    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.yourpackage.A1"/>
</activity>

<activity
    android:name="com.yourpackage.Home"  
    android:parentActivityName="com.yourpackage.A1" >

    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.yourpackage.A1"/>
</activity>

在 A2 和家庭中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Activate the UP navigation in the actionbar
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //Other stuff

}


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

    private void homeUpAction() {
        Intent upIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is NOT part of this app's task, so create a new
            // task when navigating up, with a synthesized back stack.
            TaskStackBuilder.create(this)
                    // Add all of this activity's parents to the back stack
                    .addNextIntentWithParentStack(upIntent)
                            // Navigate up to the closest parent
                    .startActivities();
        } else {
            // This activity is part of this app's task, so simply navigate
            // up to the logical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
    }
于 2014-08-18T14:36:44.073 回答