Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a fragment as default fragment for BottomNavigation?

I have a Bottom Navigation in my MainActivity.java with 3 fragments. I want to set one of my fragments as default to open automatically when app starts. how can i do that? this is my Main Activity.java:

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private BottomNavigationView bottomNavigation;
private Fragment fragment;
private android.support.v4.app.FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bottomNavigation = (BottomNavigationView)findViewById(R.id.bottom_navigation);
    bottomNavigation.inflateMenu(R.menu.bottom_menu);
    fragmentManager = getSupportFragmentManager();



    bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Fragment selectedFragment = null;
            int id = item.getItemId();
            switch (id){
                case R.id.intros:
                    fragment = new IntrosFragment();
                    break;
                case R.id.menus:
                    fragment = new MenusFragment();
                    break;
                case R.id.infos:
                    fragment = new InfosFragment();
                    break;
            }
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.main_container, fragment).commit();
            return true;
        }
    });


}
}

and this is one my fragment for example(they're all the same):

public class IntrosFragment extends Fragment {


public IntrosFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_intro, container, false);
    // Inflate the layout for this fragment
    TextView txt = (TextView) rootView.findViewById(R.id.introtv);
    Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/naskh.ttf");
    txt.setTypeface(font);

    return rootView;
}
}

what should I do?

like image 803
Arshiiia13 Avatar asked Aug 15 '17 19:08

Arshiiia13


Video Answer


1 Answers

You can leverage the fact that savedInstanceState is null the first time your activity is created to perform code only on first launch. And you can set whichever navigation item you'd like to be selected using setSelectedItemId().

Note that setSelectedItemId() will trigger your OnNavigationItemSelectedListener, so you should put this code after your call to setOnNavigationItemSelectedListener().

    if (savedInstanceState == null) {
        bottomNavigation.setSelectedItemId(R.id.infos); // change to whichever id should be default
    }
like image 91
Ben P. Avatar answered Nov 15 '22 00:11

Ben P.