Arshiiia13 Arshiiia13 -3 years ago 125
Android Question

How to set a fragment as default fragment for BottomNavigation?

I have a Bottom Navigation in my 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

public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private BottomNavigationView bottomNavigation;
private Fragment fragment;
private fragmentManager;
protected void onCreate(Bundle savedInstanceState) {
bottomNavigation = (BottomNavigationView)findViewById(;
fragmentManager = getSupportFragmentManager();

bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
int id = item.getItemId();
switch (id){
fragment = new IntrosFragment();
fragment = new MenusFragment();
fragment = new InfosFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(, fragment).commit();
return true;


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

public class IntrosFragment extends Fragment {

public IntrosFragment() {


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(;
Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "fonts/naskh.ttf");

return rootView;

what should I do?

Answer Source

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(; // change to whichever id should be default
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download