所以我有一个非常简单的项目。它包含两项 Activity 。主要 Activity 有一个抽屉导航和一个 fragment 容器。第二个 Activity 只是为了在用户与某个 fragment 交互时显示详细信息。
因此,我将主要 Activity 设置为第二个 Activity (称为“DetailsPage”)的父 Activity ,如下所示:
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailsPage"
android:label="@string/title_activity_details_page"
android:parentActivityName="com.collusion.serviceassistant.MainActivity" >>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.collusion.srviceassistant.MainActivity"/>
</activity>
在“详细信息页” Activity 代码中,我有以下内容:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.share :
share();
return true;
case R.id.home:
Log.i("BACK", "Going back!");
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 true;
}
return super.onOptionsItemSelected(item);
}
现在看来,根据我的日志,与向上操作相关的代码没有被执行。每当我点击操作栏中的向上插入符号或使用后退硬件键时,应用程序就会退出。有谁知道我在这里做错了什么?我想知道这是否与详细信息页面 Activity 不扩展 ActionBar 类有关:
public class DetailsPage extends Activity{
有人有什么想法吗?
最佳答案
根据Google docs使用您可能想要使用的主页按钮进行导航的链接
android.R.id.home instead of R.id.home
@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);
}
我认为这可能会解决主页导航问题。
关于android - 向上导航不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25472894/