How to set a fragment as default fragment for BottomNavigation?

Viewed 9431

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?

5 Answers

if you use navigation in one line:

mobile_navigation.xml

<navigation
...
    app:startDestination="@+id/navigation_home"
...
/>

Try this: In onCreate() of your container activity, put

// Default Fragment
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frameContainer, new HomeFragment())
            .commit();

Try this:

private void setDefaultFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, YOURFRAGMENT.newInstance());
        transaction.commit();

    }

And call setDefaultFragment() after setOnNavigationItemSelectedListener.

Related