我有一个应该能够导航到其父级的Activity。这就是我在Manifest中声明它的方式:
<activity
android:name="com.myapp.SeminarActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:theme="@style/Theme.AppCompat.Light"
android:parentActivityName="com.myapp.SeminarOverviewActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.myapp.SeminarOverviewActivity" />
</activity>
当我通过SeminarOverviewActivity中的Intent进入SeminarActivity时,它可以正常工作。但是可以从其他活动进入SeminarActivity。此活动有noHistory=true
,因为我不想通过向上导航返回它。当我从此活动进入SeminarActivity并按回来时,应用程序结束。我也试过使用NavUtils
并从android文档中添加了这个:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
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);
}
}
return super.onOptionsItemSelected(item);
}
但没有效果。在我的测试中,总是使用上面剪切的else部分。