Reputation: 317
after some research i need to implement hide() and show() system on my app,
now I'm doing this way:
When i select some fragment to show on my menu, i create with new Fragment(), then i use beginTransaction().replace() to replace the content on my FrameLayout with this new Fragment...
But i need to don't recreate this fragment, and i think, use the hide() and show() system, but, how do i implement this? how exactly hide() and show() works? do i need to use backstack and so?
Thank you!!
Upvotes: 1
Views: 368
Reputation: 14021
I thought I had tried the hide/show mechanism of FragmentManager. There below is one segment of my source codes implementing this. Have a look: This method to hide all visible and non-null Fragments:
private void hideAllFrags(FragmentTransaction fragmentTransaction) {
for (String name : fragNames) {
Fragment fragment = fragmentManager.findFragmentByTag(name);
if (fragment != null && !fragment.isHidden()) {
fragmentTransaction.hide(fragment);
}
}
}
And then, the key method is here:
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
hideAllFrags(fragmentTransaction);
switch (v.getId()) {
case R.id.button1:
if (fragA == null) {
fragA = new FragA();
fragmentTransaction.add(R.id.frag_container, fragA, fragNames[0]);
fragmentTransaction.addToBackStack(fragNames[0]);
} else {
fragmentTransaction.show(fragA);
}
break;
case R.id.button2:
if (fragB == null) {
fragB = new FragB();
fragmentTransaction.add(R.id.frag_container, fragB, fragNames[1]);
fragmentTransaction.addToBackStack(fragNames[1]);
} else {
fragmentTransaction.show(fragB);
}
break;
default:
break;
}
fragmentTransaction.commit();
}
I hope this will help you;)
Upvotes: 1