So I want to build an app in Android Studio that will help my family and I quit smoking. I want the app to save the current time when the button is clicked. Then I want the app to display the time of "the last time you smoked" as well as all the all the previous "last time you smoked". I'm pretty new to java, could anyone help me or guide me in the direction?
Asked
Active
Viewed 846 times
1
-
Use [SharedPreferences](http://developer.android.com/guide/topics/data/data-storage.html#pref) – OneCricketeer Feb 18 '16 at 15:56
-
This question is not up to the standard of SO. You're better off looking for a tutorial to guide you in the various aspects you need to master to get this done. Did not look through it, but the first result on google gave me this that might help. http://androidopentutorials.com/android-sharedpreferences-tutorial-and-example/ – blindstuff Feb 18 '16 at 15:59
2 Answers
1
Use SharedPreferences for that, like this:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = preferences.edit();
editor.putString("LAST_SMOKE_DATE", new Date().toString());
editor.apply();
To recover the value use this:
String dateStr = preferences.getString("LAST_SMOKE_DATE", "");
You can format your date following some tutorials on StackOverflow:
0
I recommend SQLiteOpenHelper.
SQLiteOpenHelper Android Documentation
Each time the button is pressed, insert a new record into it. And when you want it to display, retrieve the record using a cursor object.

Ophitect
- 543
- 4
- 18