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?