5

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?

1

5 Answers 5

10

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
    }
Sign up to request clarification or add additional context in comments.

1 Comment

its working but i have a problem with tabs when by default first fragment is set Now if i am again click on same tabview then my Top tabview visibility is gone why and other data is there i am using bottomNavigationview with tabviewpager
4

if you use navigation in one line:

mobile_navigation.xml

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

Comments

2

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

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

Comments

1

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);

Comments

1

Try this:

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

    }

And call setDefaultFragment() after setOnNavigationItemSelectedListener.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.