android - Saving the state of my App through rotation -
i building memory game app java class , need save words displayed through rotation of device...the widgets need saved , passed new oncreate() method button widgets used settext display words being shown....can save , pass entire state of application...here code not working , hoping insight...thanks...here code..
private bundle mybundle = new bundle(); ..... @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_eight); if(savedinstancestate != null) { savedinstancestate = mybundle; } ...... @override public void onsaveinstancestate(bundle savedinstancestate) { super.onsaveinstancestate(savedinstancestate); mybundle = savedinstancestate; }
you got semantics of using bundle backwards. don't ever want save bundle (or overwrite it). use serialize own state variables , out of it. example, if want preserve 2 integer variables , string between rotation (such can re-initialize view these members later), following:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_eight); if(savedinstancestate != null) { // restore our member variables represent activity state right here passed in bundle. _x = savedinstancestate.getint("myapp.x", 0); _y = savedinstancestate.getint("myapp.y", 0); _name = savedinstancestate.getstring("myapp.name", "player"); // not shown - initializing view elements initialized member variables. } ...... @override public void onsaveinstancestate(bundle savedinstancestate) { // save our activity state adding our member variables represent activity state bundle. savedinstancestate.putint("myapp.x", _x); savedinstancestate.putint("myapp.y", _y); savedinstancestate.putstring("myapp.name", _name); // call parent method *after* have saved state super.onsaveinstancestate(savedinstancestate); }
bundle supports arrays of simple types.
Comments
Post a Comment