java - How to maintain fragment state when switching fragments -
in mainactivity
class, have bottomnavigationview
3 tabs switch between activities (home, search, , personal). whenever click on of tabs, pulls respective fragment, believe calling new fragment each , every time.
i need these fragments maintain whatever changes made in them (similarly how instagram does). each fragment basic (newly created , unchanged), want set them in way in states saved when switch fragment , restored when go them.
below code main activity , home fragment.
public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bottomnavigationview navigationview = (bottomnavigationview) findviewbyid(r.id.bottomnavigationview); navigationview.setonnavigationitemselectedlistener(monnavigationitemselectedlistener); fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction transaction = fragmentmanager.begintransaction(); transaction.replace(r.id.content, new homefragment()).commit(); } private bottomnavigationview.onnavigationitemselectedlistener monnavigationitemselectedlistener = new bottomnavigationview.onnavigationitemselectedlistener(){ @override public boolean onnavigationitemselected(@nonnull menuitem item){ fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction transaction = fragmentmanager.begintransaction(); switch (item.getitemid()){ case r.id.homeitem: transaction.replace(r.id.content, new homefragment()).commit(); return true; case r.id.searchitem: transaction.replace(r.id.content, new searchfragment()).commit(); return true; case r.id.personalitem: transaction.replace(r.id.content, new personalfragment()).commit(); return true; } return false; } }; } public class homefragment extends fragment { // todo: rename parameter arguments, choose names match private static final string arg_param1 = "param1"; private static final string arg_param2 = "param2"; // todo: rename , change types of parameters private string mparam1; private string mparam2; private onfragmentinteractionlistener mlistener; public homefragment() { // required empty public constructor } // todo: rename , change types , number of parameters public static homefragment newinstance(string param1, string param2) { homefragment fragment = new homefragment(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mparam1 = getarguments().getstring(arg_param1); mparam2 = getarguments().getstring(arg_param2); } } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment return inflater.inflate(r.layout.fragment_home, container, false); } // todo: rename method, update argument , hook method ui event public void onbuttonpressed(uri uri) { if (mlistener != null) { mlistener.onfragmentinteraction(uri); } } @override public void onattach(context context) { super.onattach(context); if (context instanceof onfragmentinteractionlistener) { mlistener = (onfragmentinteractionlistener) context; } else { toast.maketext(context, "home fragment attached", toast.length_short).show(); } } @override public void ondetach() { super.ondetach(); mlistener = null; } public interface onfragmentinteractionlistener { // todo: update argument type , name void onfragmentinteraction(uri uri); } }
you must use transaction.hide(fragment) , transaction.show(fragment) instead of transaction.replace, since replacing delete fragment , add again, removing saved state. doing way call fragments' onpause , onreaume method, not resetting state.
hope helps!
Comments
Post a Comment