Both the answer By @David
and @ferdy182
are right but they don't told the context.
if you hide/show fragment Programmatically then use @ferdy182
and if you want to hide/show fragment which is in xml. you should follow @David
Let me explain
If you are having a single frameLayout in xml and you want to replace the other fragment in that particular one after another. use this code to add all fragment . they will be place on one another.
for(int i=0;i<4;i++)
{
getFragmentManager().beginTransaction().add(R.id.container, frag[i])
//.addToBackStack(null)
.commit();
}// add all these fragment and put them on each other then
hide all other fragment except which you want to show.
for(int j=0;j<4;j++)
{
getFragmentManager().beginTransaction().hide(frag[j]).commit();
}
getFragmentManager().beginTransaction().show(frag[0]).commit();
Benefit
These fragment work like form in c#. Forum.show and forum.hide(); . Where there current state remain their. these fragment don't call again and again. A problem here i solve it using this technique.
2nd Method
when you are having multiple frameLayout or fragment in xml . you can hide that particular by getting its id.
private GoogleMap mMap;
private SupportMapFragment mMapFragment;
mMapFragment = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapFragment));
mMap = mMapFragment.getMap();
mMapFragment.getView().setVisibility(View.INVISIBLE);
Codes
// 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();
mMap
toFragment
and execute samegetView().setVisibility(View.INVISIBLE)
, becauseSupportMapFragment
is still a fragment. – AlleviategetSupportFragmentManager().beginTransacton().hide(mMap).commit()
orgetSupportFragmentManager().beginTransacton().detach(mMap).commit()
? – Alleviateprivate GoogleMap mMap;
I'm getting: "The method hide/detach(Fragment) in the type FragmentTransaction is not applicable for the arguments (GoogleMap)" – PortiaporticogetSupportFragmentManager().beginTransacton().hide(getSupportFragmentManager().findFragmentById(R.id.mapFragment)).commit()
orgetSupportFragmentManager().findFragmentById(R.id.mapFragment).getView().setVisibility(View.INVISIBLE);
– Alleviate