Search code examples
androidfragmentback-stack

how to show hide a fragment?


I have a Activity with four buttons at the bottom like tabs. By pressing any button a new Fragment is displayed in the FrameLayout above these buttons like we do in TabActivity. See My Problem here .Now i think i should find a way to hide and show those fragments. Kindly tell me how can i show and hide a fragment without reloading it again.

Main Purpose of showing hiding a fragment is to maintain its current state. In one of my fragment i have an AsyncTask whenever i switch between fragment it call that AsynTask again.


Solution

  • // to show fragment when it is hidden

    FragmentManager fm = getSupportFragmentManager();
    fm.beginTransaction()
              .show(fragment1)
              .commit();
    

    // to hide fragment

    FragmentManager fm = getSupportFragmentManager();
    fm.beginTransaction()
              .hide(fragment1)
              .commit();