rest - Android fragments preserve content -
i using viewpager
activity
3 tabs. each tab having single fragment
display(swipable). each fragment
, data retreieved server via rest api , displayed in form of cards(using this library). hence, coding this:
public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { ... fetch_item1_card1(); //fetches data 1 , displays on card 1 fetch_item2_card2(); //fetches data 2 , displays on card 2 ... }
this working until when clicked button shows card detail in new activity
(lets activity
b). so, on pressing button, brought fragment
mentioned above(using finish();
), , same data again retrieved api.
hence thought fetch data when activity
first created. achieved doing this:
public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { ... if(savedinstancestate == null) { fetch_item1_card1(); //fetches data 1 , displays on card 1 fetch_item2_card2(); //fetches data 2 , displays on card 2 } ... }
but problem is, whenever acitivity
b (using finish();
) ,the data not there. have re-fetch manually via api.
i want fetch data ones internet, display until user re-fetch manually. how can achieve this? have kept code outside of savedinstancestate
mean lot of queries via api each time user gets activity. can minimized in other way?
in link can find explanation of problem in fact must this:
adding method:
@override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("curchoice", mcurcheckposition); }
which can use later this:
@override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); if (savedinstancestate != null) { // restore last state checked position. mcurcheckposition = savedinstancestate.getint("curchoice", 0); } }
hope works.