android - Fragment in viewpager savedinstancestate is always null -


i know question has been asked before none of answers given far of me.

i have viewpager populated fragments (android.support.v4.app.fragment) fragmentstatepageradapter . of these fragments contain logic needs retained when orientation changes, such keeping track of view selected.

however, although save data in question in onsaveinstancestate savedinstancestate null. can solve storing data in static variable (which since have 1 instance of each fragment work me) found quite ugly solution , there has proper way of doing this.

this 1 of fragments doesn't retain it's state on rotation:

    public class priceselectfragment extends fragment {      private tablerow mselected;     private int mselectedpos = 0;      // newinstance constructor creating fragment arguments     public static priceselectfragment newinstance() {         priceselectfragment fragmentfirst = new priceselectfragment();         return fragmentfirst;     }      public priceselectfragment() {         // required empty public constructor     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         // inflate layout fragment         view view = inflater.inflate(r.layout.fragment_price_select, container, false);         tablelayout mtable = (tablelayout)view.findviewbyid(r.id.price_table);          list<pricegroup> mpricegroups = ((mainactivity) getactivity()).getpricegroups();          int = 0;         (final pricegroup group : mpricegroups) {             //create row layout , access child textviews             tablerow r = (tablerow)inflater.inflate( r.layout.price_group, mtable, false);             textview size = (textview)r.getchildat(0);             textview dimension = (textview)r.getchildat(1);             textview weight = (textview)r.getchildat(2);             textview price = (textview)r.getchildat(3);              //populate row pricegroup data             size.settext(group.sizeindicator);             dimension.settext(string.format("%2.0fx%2.0fx%2.0f", group.length, group.width, group.height));             weight.settext(float.tostring(group.weight));             price.settext(integer.tostring(group.price));              //alternate background color every other row             if (i % 2 == 0) {                 r.setbackgrounddrawable(getresources().getdrawable(r.drawable.price_selector_1));             }             else {                 r.setbackgrounddrawable(getresources().getdrawable(r.drawable.price_selector_2));             }             mtable.addview(r); // add table              r.settag(i);             r.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     selectrow((tablerow) v);                 }             });             i++;         }          mselected = (tablerow)view.findviewwithtag(mselectedpos);         selectrow(mselected);          return view;     }      @override     public void onsaveinstancestate(bundle outstate) {         super.onsaveinstancestate(outstate);         outstate.putint("selected", mselectedpos);     }      @override     public void onactivitycreated(@nullable bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);         if (savedinstancestate != null) {             mselectedpos = savedinstancestate.getint("selected");         }     }      private void selectrow(tablerow row) {         if ((int) mselected.gettag() % 2 == 0) {             mselected.setbackgrounddrawable(getresources().getdrawable(r.drawable.price_selector_1));         }         else {             mselected.setbackgrounddrawable(getresources().getdrawable(r.drawable.price_selector_2));         }         mselected = row;         mselectedpos = (int) mselected.gettag();         mselected.setbackgroundcolor(getresources().getcolor(r.color.light_blue));     }   } 

how solve without having save states in static variables?

edit

i should point out of fragments programatically created , such not have id , read that might problem don't know how solve either.

also application structured this:

  • mainactivity navigationdrawer
    • fragment1
      • viewpager
        • subfragment1 - subfragment5
    • fragment2
    • fragment3

the fragments states i'm having trouble subfragments.

in activity hosting fragment need store refernce fragment in bundle.

something should work you

public void oncreate(bundle savedinstancestate) {      if (savedinstancestate != null) {         //restore fragment instance         fragment1 = getsupportfragmentmanager().getfragment(                     savedinstancestate, "fragment");     } }   protected void onsaveinstancestate(bundle outstate) {     super.onsaveinstancestate(outstate);      getsupportfragmentmanager().putfragment(outstate, "fragment", fragment1); } 

fragment1 instance of fragment1 mentioned in question needs recreated.

i haven't done structure yours before how start:

in onsaveinstancestate in fragment1 believe need same each of fragments in viewpager. in oncreateview on fragment1 fragments fragment manager , recreate viewpager.

i have found answer here pretty same has little more detail: https://stackoverflow.com/a/17135346/1417483


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -