i working on application.
somewhere want load fragment in activity , when gridview/listview item clicked on firstfragment, load secondfragment
i have added fragments way below:
mainactivity.java
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.form_creation_view); fragment fragment = new firstfragment(); fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.add(r.id.form_view, fragment).commit(); }
second fragment:
fragment fragment = new secondfragment(); fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction(); fragmenttransaction.addtobackstack(null); fragmenttransaction.replace(r.id.form_view, fragment).commit();
onbackpress of mainactivity.java :
@override public void onbackpressed() { if(getfragmentmanager().getbackstackentrycount() == 0) { super.onbackpressed(); } else { getfragmentmanager().popbackstack(); } }
so, when press backbutton on mainactivity when secondfragment loaded, switches firstfragment, calls oncreateview()
method of firstfragment again.
the firstfragment has recyclerview inside , send api request in oncreateview()
method server in order load data recyclerview.
now when come again fragment secondfragment, want request should not executed , data should populated recyclerview.
how can achieve this?
can please me here?
in oncreateview, should check this:
if(mlist.size() == 0){ //make api call }else{ //set recyclerview adapter }
this way, when come secondfragment can repopulate recyclerview.
you should consider saving list on onsaveinstancestate
Comments
Post a Comment