android - How to save data and re-open the last used Activity -
i've made majority of game's mechanics, need able to:
save data of current activity , retrieve when coming (i'd appreciate example of
sharedpreferences
if that's need)open back same
activity
left and in same time when left it.
just clearer: i want not restart main activity each time app gets closed or killed.
edit:
alright, i've used google article in order save activity , recreate later.
the code in 1 of activities following:
oncreate()
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_player_selection); // check whether we're recreating destroyed instance if (savedinstancestate != null) { // restore value of members saved state village = savedinstancestate.getstring(village); seekbarprogress = savedinstancestate.getint(progress); } else { // initialize members default values new instance initializevariables(); bundle bundle = getintent().getextras(); village = bundle.getstring(village); } [...] // other code skipped }
onsaveinstancestate()
@override public void onsaveinstancestate(bundle savedinstancestate) { // save user's current game state savedinstancestate.putstring(village, village); savedinstancestate.putint(progress, seekbarprogress); // call superclass can save view hierarchy state super.onsaveinstancestate(savedinstancestate); }
thus, i've created dispatcher activity (which main) when app killed , launched back, knows last opened activity launch. when try that, , should open (i.e.) playerselection
activity,
java.lang.nullpointerexception
as runs part of code
else { // initialize members default values new instance initializevariables(); bundle bundle = getintent().getextras(); village = bundle.getstring(village); }
instead of previous if
statement runs else
.
dispatcher
this dispatcher activity filters activity should launched:
public class dispatcher extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); class<?> activityclass; try { sharedpreferences prefs = getsharedpreferences("x", mode_private); activityclass = class.forname( prefs.getstring("lastactivity", mainactivity.class.getname())); } catch(classnotfoundexception ex) { activityclass = mainactivity.class; } startactivity(new intent(this, activityclass)); } }
this send dispatcher activity:
@override protected void onpause() { super.onpause(); sharedpreferences prefs = getsharedpreferences("x", mode_private); sharedpreferences.editor editor = prefs.edit(); editor.putstring("lastactivity", getclass().getname()); editor.commit(); } }
question
why happen? can fix this?
why happen?
the bundle
created in onsaveinstancestate
kept short-term restarts, e.g. when activity recreated due orientation change. if activity stops bundle
not kept, , can't delivered oncreate
.
what can fix this?
instead of using instancestate model, save activity state in sharedpreferences
(similar lastactivity
save). should save state in onstop
or onpause
, restore in onstart
or onresume
respectively. in cases former enough , latter produces unnecessary overhead.
@override protected void onstart() { super.onstart(); string saved = sharedpreferences.getstring("mykey", null); mypojo pojo; if(saved == null){ pojo = defaultvalue; }else { pojo = new gson().fromjson(saved, mypojo.class); } } @override protected void onstop() { sharedpreferences.edit().putstring("mykey", new gson().tojson(pojo)).apply(); super.onstop(); }
Comments
Post a Comment