I have made an application. It\'s a button that shows the time you have pressed it. Every time I \"kill\" the application, the timer starts at 0 again (naturally). How can I mak
EDIT :
public class MainActivity extends ActionBarActivity {
Button button1;
Chronometer chromo;
protected long time = 0;
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
chromo=(Chronometer)findViewById(R.id.chromo);
prefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
long savedValue = prefs.getLong("my_chrono", 0);
if(savedValue == 0)
chromo.setBase(SystemClock.elapsedRealtime());
else
chromo.setBase(SystemClock.elapsedRealtime() + savedValue);
button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
chromo.start();
}
else if( event.getAction() == MotionEvent.ACTION_UP){
time =chromo.getBase()-SystemClock.elapsedRealtime();
chromo.stop();
prefs.edit().putLong("my_chrono", time).apply();
}
return true;
}
});
}}
============================================================================
To use the shared preferences, initialize this in you onCreate
SharedPreferences prefs = getSharedPreferences("the_package_of_your_app", Context.MODE_PRIVATE);
Then, try to get the saved value
int my_saved_value = prefs.getInt("the_package_of_your_app.my_int_1", 0);
if(my_saved_value != 0)
//your value of your timer was saved, do what's needed with it
else
//there was no value saved, or the timer was at 0
Now you have to save that value when needed (when the timer is stopped, or the application is closed)
prefs.edit().putInt("the_package_of_your_app.my_int_1", my_value).apply();
To elaborate on @2Dee's answer:
SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
// We use an editor to insert values in SharedPreferences
Editor editor = prefs.edit();
// Saving the values
editor.putLong("myTime", time);
// Committing the changes
editor.commit();
can go into the
protected void onDestroy();
method. This method can be overloaded in an Activity to be called as the activity is destroyed (killed, closed, etc) so that any data may be saved (which is what you want to do).
Likewise,
SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
time = sharedpreferences.getLong("myTime", 0l);
can go into the
protected void onCreate(Bundle savedInstanceState);
method. This method is called when the activity is first created. This will set your time to the saved value (defaulting to 0 if there is none).
If for some reason these need to be called at different times (such as later or earlier in the Activity's lifecycle) you can read more about it here.
If you like this answer, please upvote 2Dee's answer as well. Some of the code is literally copy/pasted from there.
Happy Coding! Leave a comment if you have more questions.
Saving in SharedPreferences :
SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
// We use an editor to insert values in SharedPreferences
Editor editor = prefs.edit();
// Saving the values
editor.putLong("myTime", time);
// Committing the changes
editor.commit();
Retrieving saved values :
long savedValue = 0l;
SharedPreferences prefs= getSharedPreferences("prefs", Context.MODE_PRIVATE);
if (prefs.contains("hello")){
savedValue = sharedpreferences.getLong("myTime", 0l));
}
Setting values in Preference:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "slama");
editor.putInt("idName", 28);
editor.commit();
Retrieve data from preference:
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value. String name = prefs.getString("name", "");
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
and this is an Android Shared Preferences Tutorial: http://www.tutorialspoint.com/android/android_shared_preferences.htm
it can help you to solve your problem