1

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.

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

2 Answers2

5

// 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();
Orhan Obut
  • 8,756
  • 5
  • 32
  • 42
1

you can't pass by some view like

declare 4 frameLayout

private FrameLayout fragment1;
private FrameLayout fragment2;
private FrameLayout fragment3;
private FrameLayout fragment4;

and

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment1, Fragment1.newInstance());
ft.replace(R.id.fragment2, Fragment2.newInstance());
ft.replace(R.id.fragment3, Fragment3.newInstance());
ft.replace(R.id.fragment4, Fragment4.newInstance());
ft.commit();

and play with visible or gone ? like

fragment1.setVisibility(View.Visible);
fragment2.setVisibility(View.gone);
fragment3.setVisibility(View.gone);
fragment4.setVisibility(View.gone);

and by the way : works for me -> public class Activity extends ActionBarActivity {

private FrameLayout fragment1;
private FrameLayout fragment2;

private Button bt1;
private Button bt2;

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

    fragment1 = (FrameLayout) findViewById(R.id.fragment1);
    fragment2 = (FrameLayout) findViewById(R.id.fragment2);

    bt1 = (Button) findViewById(R.id.button);
    bt2 = (Button) findViewById(R.id.button2);

    bt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fragment1.setVisibility(View.VISIBLE);
            fragment2.setVisibility(View.GONE);
        }
    });

    bt2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            fragment1.setVisibility(View.GONE);
            fragment2.setVisibility(View.VISIBLE);
        }
    });

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.fragment1, new Fragment1());
    ft.replace(R.id.fragment2, new Fragment2());
    ft.commit();
}
u53r
  • 994
  • 7
  • 6
  • fragments are not views .they are object.setVisiblity is not a valid way Read the linked question http://stackoverflow.com/questions/24774631/asynctask-is-re-executing-while-switching-between-fragments – Zar E Ahmer Jul 16 '14 at 11:33
  • yes, but framelayout are view. you can plays directly with this view – u53r Jul 16 '14 at 14:16
  • i have only one frameLayout in the xml . which i want to replace with other like we do in TabActivity. And replace do two work together remove and add. I mean REPLACE==(REMOVE CURRENT FRAGMENT+ADD ANOTHER FRAGMENT) – Zar E Ahmer Jul 17 '14 at 07:12
  • i'm not sure to understand what do you really want man sorry...in this case , it's completely normal that you'r fragment reload – u53r Jul 17 '14 at 08:54