save CountDown in android studio -
my app has text view , button, when touch button, countdown start when close the app , open again countdown return 0 again. how can save countdown? shared preferences?
here code:
public class mainactivity extends activity { button b1; textview tv1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); tv1 = (textview) findviewbyid(r.id.tv1); b1 = (button) findviewbyid(r.id.b1); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu // adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @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(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public void a(view view){ new countdowntimer(10000, 1000) { public void ontick(long millisuntilfinished) { tv1.settext("la cuenta llega 0 en: " + millisuntilfinished / 1000); } public void onfinish() { tv1.settext("listo!"); } }.start(); } }
yes definitely. sharedpreferences right solution you. when close app (ondestroy
) persist value through sharedpreferences.editor
object whereas when app starts (oncreate
) load it.
examples here:
https://developer.android.com/training/basics/data-storage/shared-preferences.html