Arshiiia13
Arshiiia13

Reputation: 71

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?

Upvotes: 5

Views: 10713

Answers (5)

Acauã Pitta
Acauã Pitta

Reputation: 760

if you use navigation in one line:

mobile_navigation.xml

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

Upvotes: 4

Try this:

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

    }

And call setDefaultFragment() after setOnNavigationItemSelectedListener.

Upvotes: 1

Jeisson Valderrama
Jeisson Valderrama

Reputation: 109

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

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

Upvotes: 2

Ricardo
Ricardo

Reputation: 9696

In the BottomNavigationView library there is not a definition of a default fragment to be presented, it gives you total control of which fragment is visible by setting it programmatically as Ben pointed out.

I have implemented this not far ago and I believe you are facing the back press problem, which you probably want to go back to the "default" fragment. In my case I implemented an interface which is called onBackPressed of the activity, once the user hits the bask press just call the following method to set the "default fragment" as selected

bottomNavigationView.setSelectedItem(R.id.myDefaultTabID);

Upvotes: 1

Ben P.
Ben P.

Reputation: 54244

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
    }

Upvotes: 10

Related Questions