Android Startactivity NavigationDrawer Fragment

时间:2014-01-17 16:21:42

标签: android android-activity fragment

我正在使用带有Fragment的NaviagationDrawer。 现在在MainActivity中,我打开各种片段,但其中一行必须打开一个Activity。

/**
 * Diplaying fragment view for selected nav drawer list item
 * */
private void displayView(int position) {

    // update the main content by replacing fragments
    Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new InFragment();
        break;
    case 2:
        fragment = new RiFragment();
        break;
    case 3:
        fragment = new RieFragment();
        break;
    case 4:
        fragment = new PagesFragment();
        break;
    case 5:
        fragment = new WhatsHotFragment();
        break;

    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

在案例4中,我想像我一样开设一项活动?

1 个答案:

答案 0 :(得分:0)

如果您想在case 4:中打开一项新活动,则可以像往常一样使用Intent开始新活动。

...
case 4:
    Intent intent = new Intent(this, MyActivity.class);
    startActivity(intent);
    break;
...

您还希望更改底部的if / else块,以避免在开始新活动时记录错误(因为在这种情况下您的fragment变量将为null)。