Using SharedPreferences
package app.test;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
public class Test extends Activity {
private static final String TAG = "SavingState";
final String INITIALIZED = "initialized";
private String someString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences myPrefs = getPreferences(MODE_PRIVATE);
boolean hasPreferences = myPrefs.getBoolean(INITIALIZED, false);
if(hasPreferences) {
Log.v(TAG, "We've been called before");
someString = myPrefs.getString("someString", "");
}else {
Log.v(TAG, "First time ever being called");
someString = "some default value";
}
Editor editor = myPrefs.edit();
editor.putBoolean(INITIALIZED, true);
editor.putString("someString", someString);
editor.commit();
}
}
Related examples in the same category