android - Cant Save and Restore variable in fragment -


before post, tried read many topics restore app state , tried not solve problem.

my class is:

public class mainactivity extends actionbaractivity     implements navigationdrawerfragment.navigationdrawercallbacks {      private static boolean lv11 = true;      @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mnavigationdrawerfragment = (navigationdrawerfragment)             getsupportfragmentmanager().findfragmentbyid(r.id.navigation_drawer);     mtitle = gettitle();      // set drawer.     mnavigationdrawerfragment.setup(             r.id.navigation_drawer,             (drawerlayout) findviewbyid(r.id.drawer_layout)); }  @override public void onnavigationdraweritemselected(int position) {     // update main content replacing fragments     fragmentmanager fragmentmanager = getsupportfragmentmanager();     if(position == 0)     {         fragmentmanager.begintransaction()                 .replace(r.id.container, placeholderfragment.newinstance(position + 1))                 .commit();     }     else if(position == 1)     {         fragmentmanager.begintransaction()                 .replace(r.id.container, placeprofile.newinstance(position + 1))                 .commit();     }     else if(position == 2)     {         fragmentmanager.begintransaction()                 .replace(r.id.container, placelogbook.newinstance(position + 1))                 .commit();     }     else if(position == 3)     {         fragmentmanager.begintransaction()                 .replace(r.id.container, placebeverages.newinstance(position + 1))                 .commit();     } }  @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     super.onactivityresult(requestcode, resultcode, data);     if(resultcode == result_ok) {         boolean fininfo = data.getbooleanextra("done", false);         system.out.println("onactivityresult = " + fininfo);         if (fininfo == true) {             readit.setbackgroundcolor(r.drawable.abc_ab_bottom_solid_light_holo);             if (readit.gettext() == info1.gettext()) {                 lv11 = false;             }         }     } } public void onsectionattached(int number) {     switch (number) {         case 1:             mtitle = getstring(r.string.title_section1);             break;         case 2:             mtitle = getstring(r.string.title_section2);             break;         case 3:             mtitle = getstring(r.string.title_section3);             break;         case 4:             mtitle = getstring(r.string.title_section4);             break;     } }  public void restoreactionbar() {     actionbar actionbar = getsupportactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_standard);     actionbar.setdisplayshowtitleenabled(true);     actionbar.settitle(mtitle); }   @override public boolean oncreateoptionsmenu(menu menu) {     if (!mnavigationdrawerfragment.isdraweropen()) {         // show items in action bar relevant screen         // if drawer not showing. otherwise, let drawer         // decide show in action bar.         getmenuinflater().inflate(r.menu.main, menu);         restoreactionbar();         return true;     }     return super.oncreateoptionsmenu(menu); }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();     if (id == r.id.action_settings) {         return true;     }     return super.onoptionsitemselected(item); }  public static class placeholderfragment extends fragment {     /**      * fragment argument representing section number      * fragment.      */     private static final string arg_section_number = "section_number";      /**      * returns new instance of fragment given section      * number.      */     public static placeholderfragment newinstance(int sectionnumber) {         placeholderfragment fragment = new placeholderfragment();         bundle args = new bundle();         args.putint(arg_section_number, sectionnumber);         fragment.setarguments(args);         return fragment;     }      public placeholderfragment() {     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.fragment_main, container, false);         initializeitem();                     if(savedinstancestate != null) {             log.v("","on creat view");             lv11 =  savedinstancestate.getboolean("level1");             if (lv11 == false) {                 info1.setbackgroundcolor(r.drawable.abc_ab_bottom_solid_light_holo);             }         }           //where create view ...          return rootview;        }      @override     public void onsaveinstancestate(bundle savedinstancestate) {          log.v("tag", "in frag's on save instance state ");         savedinstancestate.putboolean("level1",false);         super.onsaveinstancestate(savedinstancestate);     }      @override     public void onattach(activity activity) {         super.onattach(activity);         ((mainactivity) activity).onsectionattached(                 getarguments().getint(arg_section_number));     } } 

all working fine problem using onsaveinstantstate() method.i used log.v check , called when press recent button on hardware kill it.after when run app again saveinstantstate null.in case want change button color check boolean put parameter in save state , restore on oncreateview.

i want know:

  1. what wrong in implementation , why can't call restore state
  2. how implement in right way

*all code above in same class file

thank you,and sorry grammar.

after killed application, bundles destroyed. onsavedinstancestate() used protect state of app being killed os. in case need save state permanently. "sharedpreferences" ideal that.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -