简体   繁体   中英

How to set a default fragment shown in the main activity?

So i've encountered a small problem today. I was making a bottom navigation view in my app, and after clicking buttons, it replaces the fragment on the screen (and it works perfectly!). But just after launching the app, and without clicking any button, there is no fragment on the screen. I've realized that the fragments are shown only after clicking a button, and I'd like to have a default fragment (kalkulatorFragment). I've been trying my best to somehow set it up, but no success...

public class Main extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        kalkulatorFragment kalkulator_fragment = new kalkulatorFragment();
        wzoryFragment wzory_fragment = new wzoryFragment();
        definicjeFragment definicje_fragment = new definicjeFragment();

            switch (item.getItemId()) {
                case R.id.kalkulator:
                    ft.replace(android.R.id.content, kalkulator_fragment);
                    ft.commit();
                    return true;

                case R.id.wzory:
                    ft.replace(android.R.id.content, wzory_fragment);
                    ft.commit();
                    return true;

                case R.id.definicje:
                    ft.replace(android.R.id.content, definicje_fragment);
                    ft.commit();
                    return true;
            }
            return false;
        }
   

Ok i just figured it out. I moved the ft.replace to the onCreate() method, so the kalkulatorFragment is going to be shown just after creating an Activity.

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    kalkulatorFragment kalkulator_fragment = new kalkulatorFragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    ft.replace(android.R.id.content, kalkulator_fragment);
    ft.commit();

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

You need to use this code OUTSIDE of OnCreate Method:

navigation.setSelectedItemId(R.id.IdOFYourItemFromBottomNavigationMenuItems);

I don't know why, but it wont work inside OnCreate method. You can declare and initialize it inside OnCreate method, just can't set the default item in there.

In my case I am using it inside OnCreateOptionsMenu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM