android - Close app and reopen Continue read text -
this simple code multi text in 1 textview,
int stringidlist[] = {r.string.text1, r.string.text2, r.string.text3, r.string.text4}; int stringlistcounter = 0; textview text1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button next = (button) findviewbyid(r.id.next1); button previous = (button) findviewbyid(r.id.pre1); text1 = (textview) findviewbyid(r.id.textview1); next.setonclicklistener(this); previous.setonclicklistener(this); @override public void onclick(view v) { int id = v.getid(); if(id == r.id.next1 && stringlistcounter < stringidlist.length - 1) { stringlistcounter++; } else if (id == r.id.pre1 && stringlistcounter > 0) { stringlistcounter--; } text1.settext(stringidlist[stringlistcounter]); }
my problem :when close app , again reopen app want continue last text read before close apps when close , reopen app begin first text now.
u should use sharedpreference class save text index.
@override public void onpause() { super.onpause(); sharedpreferences pref = preferencemanager.getdefaultsharedpreferences(getactivity()); pref.edit().putint("stringlistcounter", stringlistcounter).commit(); } @override public void onresume() { super.onresume(); sharedpreferences pref = preferencemanager.getdefaultsharedpreferences(getactivity()); stringlistcounter = pref.getint("stringlistcounter", 0); }
Comments
Post a Comment