您将需要使用 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);
}
}