Set Initial Fragment on startup

ぐ巨炮叔叔 提交于 2019-11-29 12:34:39

Write this in the onCreate(Bundle savedInstanceState) method after you set your drawer up:

mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
        (DrawerLayout) findViewById(R.id.drawer_layout));

if (savedInstanceState == null) {
    // on first time to display view for first navigation item based on the number
    onSectionAttached(2); // 2 is your fragment's number for "CollectionFragment"
}

Notice that your Fragment's number is 2. The number is got from your public void onSectionAttached(int number) method. If case 2: opens the CollectionFragment, so your Fragment's number is 2 (because of case number is 2). Then, alter your onSectionAttached() slightly:

public void onSectionAttached(int number) {
    // update the main content by replacing fragments
    Fragment fragment = null;

    switch (number) {
        case 1:
            mTitle = "Home";
            break;
        case 2:
            mTitle = "My Information";
            fragment = new CollectionFragment();
            break;
        case 3:
            mTitle = "My Display";
            fragment = new Display();
            break;
        case 4:
            mTitle = "Donate";
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.org"));
            startActivity(browserIntent);
            break;
        case 5:
            mTitle = "Settings";
            break;
        case 6:
            mTitle = "Log Out";
            ParseUser.logOut();
            fragment = new Authentication();
            break;
        default:
            break;
        }

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

        } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

So now, you just need to call onSectionAttached(your fragment's number); to display your Fragment you want.

To handle the default selection of the fragment in Your MainActivity, you need to do the following:

  • In the MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    int selectedSection = getIntent().getIntExtra(EXTRA_PARAMS_ID, 0);

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout), selectedSection);
    mNavigationDrawerFragment.selectItem(selectedSection);
}


@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    // You could switch over different positions to show the right fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, YourFragment.newInstance(position))
                    .commit();
}

public void onSectionAttached(int position) {
    final String[] sections = getResources().getStringArray(R.array.sections);
    if (sections.length > position)
        mTitle = sections[position];
    getSupportActionBar().setTitle(mTitle);
}
  • In the NavigationDrawerFragment remove the selectItem(mCurrentSelectedPosition); from the onCreate()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!