1

我在 ViewPager 中有 ListFragments,每个ListFragment都包含在一个活动中,并且有自己的选项菜单项,用户可以单击/点击以启动名为addEditItem. 现在我想在活动中提供向上导航,在该addEditItem活动中按下主页按钮(向上按钮)将返回到ListFragment. 我不知道我们是否可以将 a 指定Fragment为 an 的父级,Activity因为我们可以指定活动。我在optionsItemSelectedItem内部addEditItem活动中尝试的内容如下(但这是通过NullPointerException at navigateUpTo):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    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.
            Log.i(MyConstants.LOG_TAG, "navigate up to");
            try {

                NavUtils.navigateUpTo(this, upIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;        

    default:
        break;
    }

    return super.onOptionsItemSelected(item);
}
4

0 回答 0