It can be tricky or frustrating when your application needs to share data between two or more activities in a very simple way. I am saying this from my own experince especially when I started developing Android apps. Soon you will realize how simple it is to store strings or numbers and retrieve between activities.
By creating SharedPreferences object, you can accomplish just that and your data will be persistance. Depending on what kind of application you are doing, the data can be anything from log in information, todo list or anything that can be stored in the key/value form.
To illustrate this, look at the two activities below. The first activity creates a shared file that will be accessed with two or more activities. I do assume that you can create a new project since I will not cover that part.
Create your first activity and give it any name. My activity keeps track of team’s score. Layout file is not shown in the example but I will reference each view that is in the layout.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
public class TeamScore extends Activity implements OnClickListener{ EditText etTeam, etScore; Button btnSave; String teamName; int teamScore; //create a file name as a constant - very important public static final String PREF_NAME = "myScores"; //create a SharedPreferences object private SharedPreferences score; @Override public void onCreate() { super.onCreate(savedInstanceState); setContentView(R.layout.addScore); init(); score = getSharedPreferences(PREF_NAME, Activity.MODE_PRIVATE); } public void init() { etTeam = (EditText) findViewById(R.id.team); etScore = (EditText) findViewById(R.id.score); btnSave = (Button) findViewById(R.id.save); btnSave.setOnclickListener(this); } @override public void onClick(View view){ //access editor so that you can edit the file SharedPreferences.Editor editor = score.edit(); teamName = etTeam.getText().toString(); teamScore = etScore.getText(); //store your data in a key/value form editor.putString(teamName, teamScore); editor.commit(); } } |
Now lets access scores stored in SharedPreferences object. Again, layout file is not included below but I am going to initiate and reference all the views from it. Pay attention on how I access the SharedPreferences file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public class ShowScore extends Activity implements OnClickListener{ EditText teamName; Button showScore; String name; int teamScore; //create a SharedPreferences object private SharedPreferences score; @Override public void onCreate() { super.onCreate(savedInstanceState); setContentView(R.layout.checkScore); init(); //access SharedPreferences from TeamScore static variable score = getSharedPreferences(TeamScore.PREF_NAME, Activity.MODE_PRIVATE); } public void init() { teamName = (EditText) findViewById(R.id.name); showScore = (Button) findViewById(R.id.show); showScore.setOnclickListener(this); } @override public void onClick(View view){ //access editor so that you can retrieve data SharedPreferences.Editor editor = score.edit(); name = teamName.getText().toString(); //retrieve score data from SharedPreferences using "name" as the key. teamScore = editor.getInt(name, "Team not found"); //show score as toast Toast.makeText(ShowScore.this, teamScore, Toast.LENGTH_SHORT).show(); } } |
To show a score for a specific team, a user will type in team’s name and press the “Show Score” button. If a user misspelled team’s name or one that’s not stored, the “Team not found” message will be displayed.
As you can see, shared preferences between activity above has been accomplished by accessing the file name that is declared as constant in TeamScore activity. All data stored in the file can be accessed by creating SharedPreferences the same way. This will not work for Shared Preferences between applications.
Pingback: SharedPreferences Across Applications | ZaidiSoft