How to select the first item in a navigation drawer and open a fragment on application start

前端 未结 8 613
鱼传尺愫
鱼传尺愫 2021-02-04 05:32

I have created MainActivity with NavigationView. When Activity is opened I want to automatically select the first item in the navigation d

8条回答
  •  再見小時候
    2021-02-04 06:19

    1.) To land to the HomeFragment initially, use this inside your onCreate() in MainActivity:

    Fragment fragment = new HomeFragment();
    // replacing the fragment
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }
    

    2.) To set the item as selected in navigationDrawer set the item as checked in navigation_menu.xml

     android:checked = "true"
    

提交回复
热议问题