Set Initial Fragment on startup

前端 未结 2 932
滥情空心
滥情空心 2020-12-20 07:04

I am trying to set which fragment should be displayed first on start up. To determine this I check whether the user is logged in with ParseUser.getCurrentUser() and if a fil

2条回答
  •  -上瘾入骨i
    2020-12-20 08:02

    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()

提交回复
热议问题