java - I'm unsure on how to save my float value on pause/destroy of the app -


i have app clicking image makes value (goldcount) increase 1. when run, app runs splash, before starting app. however, should minimize or close game, goldcount value reverts 0.0, , tutorials on sharedpreferences giving me no clues how may go saving float value.

my main java code:

package com.bipbapapps.leagueclickerapp;  import android.app.activity; import android.graphics.typeface; import android.os.bundle; import android.view.gravity; import android.view.view; import android.view.view.onclicklistener; import android.view.window; import android.view.windowmanager; import android.widget.button; import android.widget.textview;  public class mainclass extends activity implements onclicklistener {  public float goldcount = 0.0f; button minionclick; textview textgoldcount; string texttotal;  @override public void oncreate (bundle savedinstancestate) {     super.oncreate(savedinstancestate);          //set fullscreen     requestwindowfeature(window.feature_no_title);     getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen);      setcontentview(r.layout.mainlayout);        //linking variables     minionclick = (button) findviewbyid(r.id.minioncentreid);     textgoldcount = (textview) findviewbyid(r.id.textviewtop);      //string display @ top of app     texttotal = goldcount + " gold";      //setting textview string     textgoldcount.settext(texttotal);     textgoldcount.setgravity(gravity.center);     typeface tf = typeface.createfromasset(getassets(), "mechanical.ttf");     textgoldcount.settypeface(tf);     textgoldcount.settextsize(35);      //setting onclicklistener     minionclick.setclickable(true);      minionclick.setonclicklistener(this);  }   @override public void onclick(view v) {     // todo auto-generated method stub     switch (v.getid()){     case r.id.minioncentreid:     goldcount += 1.0;     texttotal = goldcount + " gold";     textgoldcount.settext(texttotal);     textgoldcount.setgravity(gravity.center);     break;     }   } 

}

my splash code, if it's relevant:

package com.bipbapapps.leagueclickerapp;  import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.window; import android.view.windowmanager;   public class splash extends activity {  @override public void oncreate(bundle splashbundle) {      // todo auto-generated method stub     super.oncreate(splashbundle);      requestwindowfeature(window.feature_no_title);     getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen);     setcontentview(r.layout.splash);        thread logotimer = new thread(){         public void run(){             try {                 sleep(2000);                 intent mainintent = new intent("com.bipbapapps.leagueclickerapp.clicker");                 startactivity(mainintent);             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();             }              {             finish();                }         }     };     logotimer.start(); }  @override protected void onpause() {     // todo auto-generated method stub     super.onpause(); } 

}

i grateful if me this.

not related question, in splash code, instead of creating new thread, following:

new handler().postdelayed(new runnable() {     public void run() {         intent mainintent = new intent("com.bipbapapps.leagueclickerapp.clicker");         startactivity(mainintent);     } }, 2000); 

it cleaner , preferred in cases when defining delayed action.

to answer question, seems not dealing save value of goldcount.

at top of class following. "app_name_here" should replaced application name, or sort of key matter.

sharedpreferences prefs = getsharedpreferences("app_name_here", context.mode_private); 

in oncreate method, following:

public void oncreate(bundle savedinstancestate) {     // other oncreatestuff      goldcount = prefs.getfloat("goldcount", 0.0f);      // other oncreatestuff } 

and in onclick method:

@override public void onclick(view v) {     switch (v.getid()){         case r.id.minioncentreid:             goldcount += 1.0;             prefs.edit().putfloat("goldcount", goldcount).commit();             texttotal = goldcount + " gold";             textgoldcount.settext(texttotal);             textgoldcount.setgravity(gravity.center);         break;     } } 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -