There are a few ways to save data persistently between launches of your Android application. One way is to actually use a database, but I felt this was overkill for my needs since I only needed to store two numbers. The other way is to save it as an Android application preference, which only needs a few lines of code. To do this, you simply make use of the SharedPreferences object which can be accessed within any Activity. It will store the data to the Android OS referenced by a key that uses your application name as its prefix by default (there is another call you can make if you want to specify the name and I believe this is so you can share persistent data between two different apps but that’s not needed here). You can then retrieve it at any time.
Place the call to save the data in the onPause function which gets called right before the Activity is closed and you can access the saved again in any other function.
public class MyApp extends Activity {
private SharedPreferences mPrefs;
private int mMonth;
private int mYear;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPrefs = getPreferences(MODE_PRIVATE);
mMonth = mPrefs.getInt("month", 1);
mYear = mPrefs.getInt("year", 2011);
}
protected void onPause() {
super.onPause();
SharedPreferences.Editor ed = mPrefs.edit();
ed.putInt("month", mMonth);
ed.putInt("year", mYear);
ed.commit();
}
}
Note that you will be able to specify a default value in getInt as the second argument when you retrieve a saved preference because the first time it ever gets called it won’t exist yet. In this sample code, the month value defaults to 1 and the year defaults to 2011. Once data is saved the very first time, these default values won’t be used again.
On a side note–if you are using month data–don’t use 1 because it’s actually February if you are using the Calendar class and not January as expected, so use the Calendar.JANUARY enumeration.

