简体   繁体   中英

Why show and hide Fragments with BottomNavigation view is not woking?

I am working with BottomNavigationView and 5 fragments in it. I want to each fragment create only one time and doesn't recreate each time the user switch between them.

Because of that I am creating all the fragments and try to show and hide them in the fragment container.

This is my code: (I write this code with this help )

public class MainActivity extends AppCompatActivity {

  private Fragment ideaFragment = IdeaFragment.newInstance();
  private Fragment articleFragment = ArticleFragment.newInstance();
  private Fragment videoFragment = VideoFragment.newInstance();
  private Fragment magFragment = MagFragment.newInstance();
  private Fragment mainListFragment = MainListFragment.newInstance();


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(view);


    btmNavigationView.setSelectedItemId(R.id.nav_home);
    btmNavigationView.setOnNavigationItemSelectedListener(navListener);

    fm = ((FragmentActivity) activity).getSupportFragmentManager();

    MainPresenter mainPresenter = new MainPresenter((MainListContract.View) mainListFragment, appRepository);
    ArticlePresenter articlePresenter = new ArticlePresenter((ArticleContract.View) articleFragment, appRepository);
    IdeaPresenter ideaPresenter = new IdeaPresenter((IdeaContract.View) ideaFragment, appRepository);
    VideoPresenter videoPresenter = new VideoPresenter((VideoContract.View) videoFragment, appRepository);
    MagPresenter magPresenter = new MagPresenter((MagContract.View) magFragment, appRepository);


    fm.beginTransaction().add(R.id.fragment_container, magFragment, "5").commit();
    fm.beginTransaction().add(R.id.fragment_container, ideaFragment, "4").commit();
    fm.beginTransaction().add(R.id.fragment_container, videoFragment, "3").commit();
    fm.beginTransaction().add(R.id.fragment_container, articleFragment, "2").commit();
    fm.beginTransaction().add(R.id.fragment_container, mainListFragment, "1").commit();

  }

  //setting up bottom navigation menu
  BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

      //Fragment currentFragment = ((FragmentActivity) activity).getSupportFragmentManager().findFragmentById(R.id.fragment_container);


      switch (menuItem.getItemId()) {

        case R.id.nav_home:

          if (active != mainListFragment)
            fm.beginTransaction().show(mainListFragment).commit();
          else
            fm.beginTransaction().hide(active).show(mainListFragment).commit();
          active = mainListFragment;


        case R.id.nav_article:

          if (active != articleFragment)
            fm.beginTransaction().show(articleFragment).commit();
          else
            fm.beginTransaction().hide(active).show(articleFragment).commit();
          active = articleFragment;


        case R.id.nav_video:

          if (active != videoFragment)
            fm.beginTransaction().show(videoFragment).commit();
          else
            fm.beginTransaction().hide(active).show(videoFragment).commit();
          active = videoFragment;


        case R.id.nav_idea:

          if (active != ideaFragment)
            fm.beginTransaction().show(ideaFragment).commit();
          else
            fm.beginTransaction().hide(active).show(ideaFragment).commit();
          active = ideaFragment;


        case R.id.nav_mag:

          if (active != magFragment)
            fm.beginTransaction().show(magFragment).commit();
          else
            fm.beginTransaction().hide(active).show(magFragment).commit();
          active = magFragment;

      }

      return true;
    }
  };
}

My problem is that MainListFragment will show when opening the application but when I try to go to other fragments nothing happens! And MainListFragment will not change!

Where am I wrong?

Thankyou for your answers.

Leave only the main one with a replace instead of the add :

fm.beginTransaction().replace(R.id.fragment_container, magFragment, "5").commit();

and remove this from the onCreate

fm.beginTransaction().add(R.id.fragment_container, ideaFragment, "4").commit();
fm.beginTransaction().add(R.id.fragment_container, videoFragment, "3").commit();
fm.beginTransaction().add(R.id.fragment_container, articleFragment, "2").commit();
fm.beginTransaction().add(R.id.fragment_container, mainListFragment, "1").commit();

then, in the on onNavigationItemSelected do the follow:

switch (menuItem.getItemId()) {
    case R.id.nav_home:
        fm.beginTransaction().replace(R.id.fragment_container, magFragment, "5").commit();
        break;
    case R.id.nav_article:
        fm.beginTransaction().replace(R.id.fragment_container, articleFragment, "2").commit();
        break;
    case R.id.nav_video:
        fm.beginTransaction().replaceR.id.fragment_container, videoFragment, "3").commit();
        break;
    case R.id.nav_idea:
        fm.beginTransaction().replace(R.id.fragment_container, ideaFragment, "4").commit();
        break;
    case R.id.nav_mag:
        fm.beginTransaction().replace(R.id.fragment_container, mainListFragment, "1").commit();
        break;
}

Instantiating every fragment in the onCreate is the right way, in this way, you won't create another fragment everytime you switch. If you want to achieve that behavior you have to use something like this:

fm.beginTransaction().replace(R.id.fragment_container, new MagPresenter((MagContract.View) magFragment, appRepository), "1").commit();

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