step 1 : Get a Handle to a SharedPreferences
Context context = getActivity(); SharedPreferences sharedPref = context.getSharedPreferences( getString(R.string.preference_file_key), Context.MODE_PRIVATE);
step 2 : Write to Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putInt(getString(R.string.saved_high_score), newHighScore); editor.commit();
step 3 : Read from Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); int defaultValue = getResources().getInteger(R.string.saved_high_score_default); long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
Practice:
step 1 : create layout xml for MainActivity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/oldcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:hint="@string/hello_world"
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnText"/>
</LinearLayout>
step 2 : modify MainActivity.java
package com.example.sharedpreferences;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button btn_save;
private EditText input;
private TextView oldContent;
private SharedPreferences sharedPreferences = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences(this.getApplication().getPackageName(),
Context.MODE_PRIVATE);
setContentView(R.layout.activity_main);
initComponents();
}
private void initComponents() {
btn_save = (Button) findViewById(R.id.btnSave);
btn_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Editor editor = sharedPreferences.edit();
editor.putString(getString(R.id.oldcontent), input.getText()
.toString());
editor.commit();
oldContent.setText(input.getText());
}
});
input = (EditText) findViewById(R.id.editText);
oldContent = (TextView) findViewById(R.id.oldcontent);
String oldText = sharedPreferences.getString(
getString(R.id.oldcontent), "default");
oldContent.setText(oldText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}