Home > Blockchain >  In Bottom Navigation Fragment is not load for first time
In Bottom Navigation Fragment is not load for first time

Time:12-11

I have five fragments in main activity for Bottom_Navigation_view .When I load fragments/MainActivity for the first time. The first fragment (home) not shown for the first time, when I swipe to other three fragments and then get back to the my first fragment(home) then the data/fragment is shown

I want to load first fragment(Home) for the first time user comes and Bottom Navigation/Main Activity loads

    //Bottom Navigation's
    BottomNavigationView bnv =findViewById(R.id.bottom_nav);
    bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id=item.getItemId();
            if (id == R.id.home) {
               loadfrag(new Home(),false);
            }else  if (id == R.id.search) {
                loadfrag(new search(),true);
            }else  if (id == R.id.Fav) {
                loadfrag(new fav(),true);
            }else  if (id == R.id.Cart) {
                loadfrag(new Cart(),true);
            }else  if (id == R.id.User) {
                loadfrag(new Userprofile(),true);
            }
            return true;
        }
    });
//My first fragment
bnv.setSelectedItemId(R.id.home);
}
//load_fragment method
public void loadfrag(Fragment fragment,Boolean flag){
    FragmentManager fm=getSupportFragmentManager();
    FragmentTransaction ft=fm.beginTransaction();
    if (!flag) {
        ft.add(R.id.container,fragment);
    }else {
        ft.replace(R.id.container,fragment);
    }
    ft.commit();
}

CodePudding user response:

Try this once you missed one default line

loadfrag(new Home(),false);

//Bottom Navigation's
    BottomNavigationView bnv =findViewById(R.id.bottom_nav);
    bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id=item.getItemId();
            if (id == R.id.home) {
               loadfrag(new Home(),false);
            }else  if (id == R.id.search) {
                loadfrag(new search(),true);
            }else  if (id == R.id.Fav) {
                loadfrag(new fav(),true);
            }else  if (id == R.id.Cart) {
                loadfrag(new Cart(),true);
            }else  if (id == R.id.User) {
                loadfrag(new Userprofile(),true);
            }
            return true;
        }
    });
   loadfrag(new Home(),false);
//My first fragment
bnv.setSelectedItemId(R.id.home);
}
//load_fragment method
public void loadfrag(Fragment fragment,Boolean flag){
    FragmentManager fm=getSupportFragmentManager();
    FragmentTransaction ft=fm.beginTransaction();
    if (!flag) {
        ft.add(R.id.container,fragment);
    }else {
        ft.replace(R.id.container,fragment);
    }
    ft.commit();
}
  • Related