CRTC Electronics

Click here to edit subtitle

Saving Settings and Other Data

Learn how to save settings and data from a text field.

Set up

In onCreate make a SharedPreferences object called "settings"

SharedPreferences settings_ = getSharedPreferences("crtcSecuritySettings", 0); 

Read saved data

after creating the  SharedPreferences  and a textfield, in onCreate use .getString,  It retrieves a string from memory called what ever is in the first argument, if nothing is found the second argument is used a a default.

settings_.getString("movedMessage", "Something Moved!");


use .setText() set the text in the the textbox

etMessage_ .setText("My Message");

Read and Write Combined

Read and write combined may look something like this: 
mytextbox_ .setText(settings.getString("message", "Something Moved!"));

At this point  Summery

In summery, this code should be in your onCreate  funtion

SharedPreferences settings_ = getSharedPreferences("crtcSecuritySettings", 0); 

etMessage_ .setText(settings_.getString("message", "Something Moved!"));

Saving Data

put this code in when you want to save. 

SharedPreferences settings = getSharedPreferences("crtcSecuritySettings",0);

SharedPreferences.Editor editor = settings.edit();

editor.putString("message", etMessage_.getText().toString());

editor.putString("number", etPhone_.getText().toString());


more info: