Thursday 12 September 2013

Saving Activity state in Android using Shared Preferences

How to save the activity instance using Shared Preferences


As you all know that there are many situations where we need to store the instance of the activity so that when the user returns to the application,the instance should remain the same.

The best example is whenever you play the game, the Highest score is changed when you create a new highest score. And whenever you return the game the score is the new high score.

So we'll learn the new concept of Shared Preferences i.e used to store the value in the application database till the application is uninstalled.

1.activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:inputType="text">

    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_marginRight="19dp"
        android:layout_marginTop="38dp"
        android:text="TextView" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:text="Register the Score" />

</RelativeLayout>

2. MainActivity.java

package com.sri.score;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText e1;
Button b;
TextView tv;
int i;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
e1=(EditText) findViewById(R.id.editText1);
b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
   i = Integer.parseInt(e1.getText().toString());
   tv.setText("the score changed to " + i);
   Toast.makeText(getApplicationContext(), "score registered", Toast.LENGTH_SHORT).show();
}
});
if (savedInstanceState == null)
i = 0;
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
// Set the values of the UI
int j = preferences.getInt("i", i);
tv.setText("the score now is " + j);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
// Put the values from the UI
editor.putInt("i", i);
editor.commit();
}

}


3.Run the application.
First your application value is 0, next your new score is registered. exit the application and open app once again to see the value remains as you exited previously.

No comments:

Post a Comment