Android Fragment Back Press Without Any Data Loss
I am creating an application with navigation drawer when I click each navigation list new fragments will load. One of the fragment have listView. When I click this list load anothe
Solution 1:
I think this may help
Just Do PopBackStack
from Fragmentmanager
FragmentManagerfm= getFragmentManager();
for (inti=0; i < fm.getBackStackEntryCount(); i++) {
fm.popBackStack();
}
And In that Fragment
View mView ; //this will be global@Nullable@OverridepublicViewonCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
try {
if (mView == null) {
// view will be initialize for the first time .. you can out condition for that if data is not null then do not initialize view again.
mView = inflater.inflate(R.layout.xml,container, false);
}
} catch (Exception e) {
e.printStackTrace();
}
return mView;
}
Solution 2:
publicvoidaddFragment( final Fragment newFragment, final Fragment hideFragment) {
finalFragmentManagerfragmentManager= getFragmentManager();
finalFragmentTransactionfragmentTransaction= fragmentManager.beginTransaction();
fragmentTransaction.hide(hideFragment);
fragmentTransaction.add(R.id.container, newFragment, newFragment.getClass().getSimpleName());
fragmentTransaction.addToBackStack(hideFragment.getClass().getSimpleName());
fragmentTransaction.commitAllowingStateLoss();
}
Use this instead of replacing fragment.will solve your problem.
Post a Comment for "Android Fragment Back Press Without Any Data Loss"