android - how to set the value of sharedPreferences in other activity? -
i have 2 activities in app. first activity splashscreen , other splashactivity. have sharedpreferences in splashscreen want set value of true in splashactivity. think if possible create method in splashactivity run once i.e method compare boolean value of splashscreen (like false @ start). after first run set true forever , method skipped. i've tried lot not successful.
splashscreen-
public class splashscreen extends activity { /** called when activity first created. */ timertask task; sharedpreferences pref; sharedpreferences.editor ed; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.splash_screen); pref = getsharedpreferences("activitypref", context.mode_private); log.v("","oncreate calling"); if(pref.getboolean("activity_executed", false)) { log.v("","before if called"); setcontentview(r.layout.splash_screen); log.v("","after if called"); new handler().postdelayed(csrunnable1, 5000); } else { new handler().postdelayed(csrunnable2, 5000); } } runnable csrunnable1=new runnable() { @override public void run() { intent intent = new intent(splashscreen.this, splashactivity.class); startactivity(intent); finish(); } }; runnable csrunnable2=new runnable() { @override public void run() { intent intent = new intent(splashscreen.this, loginactivity.class); startactivity(intent); finish(); } }; } splashactivity-
public class splashactivity extends activity implements onclicklistener { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.welcome); ////////////////////////////////////////////////////////////////////// //////// game menu ///////////////////////////////////////////////// button playbtn = (button) findviewbyid(r.id.playbtn); playbtn.setonclicklistener(this); button settingsbtn = (button) findviewbyid(r.id.settingsbtn); settingsbtn.setonclicklistener(this); button rulesbtn = (button) findviewbyid(r.id.rulesbtn); rulesbtn.setonclicklistener(this); button exitbtn = (button) findviewbyid(r.id.exitbtn); exitbtn.setonclicklistener(this); } /** * listener game menu */ @override public void onclick(view v) { intent i; switch (v.getid()){ case r.id.playbtn : //once logged in, load main page //log.d("login", "user has started game"); //get question set // list<question> questions = getquestionsetfromdb(); //initialise game retrieved question set /// gameplay c = new gameplay(); c.setquestions(questions); c.setnumrounds(getnumquestions()); ((cykapplication)getapplication()).setcurrentgame(c); //start game now.. // = new intent(this, questionactivity.class); startactivityforresult(i, constants.playbutton); finish(); break; case r.id.rulesbtn : = new intent(this, rulesactivity.class); startactivityforresult(i, constants.rulesbutton); finish(); break; case r.id.settingsbtn : = new intent(this, settingsactivity.class); startactivityforresult(i, constants.settingsbutton); finish(); break; case r.id.exitbtn : finish(); break; } } i know line need edit in splashactivity whenever add app crash.
ed = pref.edit(); ed.putboolean("activity_executed", true); ed.commit();
may doing wrong. can in splash screen
if(pref.getboolean("activity_executed", false)) { log.v("","before if called"); setcontentview(r.layout.splash_screen); log.v("","after if called"); new handler().postdelayed(csrunnable1, 5000); pref.edit().putboolean("activity_executed", true).commit(); } or in splashactivity oncreate call this
getsharedpreferences("activitypref", context.mode_private).edit().putboolean("activity_executed", true).commit();
Comments
Post a Comment