Mohit
Mohit

Reputation: 1063

Using SharedPreferences for saving Application's Setting

Lets say, my app has 2 activities.

  1. Activity 1, the main activity with a button which opens Activity 2.
  2. Activity 2, which is my setting activity and has a checkBox.

Now I am saving the value of, if the checkbox is checked or not, using SharedPreferences.

Now I want to use this setting value in every other activity, so I am making a toast in activity 1 which displays if the checkBox is checked or not.

Code in activity 1:-

if(new Activity2().getCheckStatus())
    Toast.makeText(getApplicationContext(), "chcked", Toast.LENGTH_SHORT).show();
else
    Toast.makeText(getApplicationContext(), "not chcked", Toast.LENGTH_SHORT).show();

Activity 2:-

public class Activity2 extends Activity implements OnClickListener
{
    CheckBox check =null;

    SharedPreferences settings;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        getActionBar().setTitle("SETTING");

        setContentView(R.layout.setting);

         check =(CheckBox)findViewById(R.id.checkBox);
         check.setOnClickListener(this);

         settings = Activty2.this.getSharedPreferences("checkBox",0);

        Boolean a = settings.getBoolean("isChecked", false);
            check.setChecked(a);
    }

    /* (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {

        case R.id.checkBox:


            SharedPreferences.Editor editor = settings.edit();

            if(!check.isChecked())
            {
                check.setChecked(true);
                editor.putBoolean("isChecked", true);
                editor.commit();
            }
            else
            {
                check.setChecked(false);
                editor.putBoolean("isChecked", false);
                editor.commit();
            }
            break;
        }
    }

    public boolean getCheckStatus() 
    {
        SharedPreferences mysettings;
        try{
             mysettings = Activity2.this.getSharedPreferences("checkBox",0);
        }catch(NullPointerException e)
        {
            return false;
        }

        return mysettings.getBoolean("isChecked", false);


    }
}

Activity2 is just for showing if the checkBox is checked or not, if it is then show so and if there is any changes then save it using SharedPreferences.

My reason for catching NullPointerException is, as I am showing toast in activity1, which is my main activity, so when ever the app is run first time, there will be no SharedPref until I have run onCreate() of activity2. So if I am getting the exception it means the app is running for the first time or the user has not until now opened Activity2. If so return the default false value, if not then catch() is never called and I get the value stored.

MyProblem:- As you guessed, It's not working. The checkBox is working fine,whenever I open Activity2 it shows me the previous set value but the toast is always showing not checkd .

Please tell me , whats the error here or is there any other simpler way to save android app user settings and access them anywhere else in the app.

Thanks.

Upvotes: 0

Views: 153

Answers (2)

Kelevandos
Kelevandos

Reputation: 7082

First of all, move the SharedPrefs read to activity1, as this

if(new Activity2().getCheckStatus())

will be deadly if Activity 2 is bigger.

So this:

public boolean getCheckStatus() 
{
    SharedPreferences mysettings;
    try{
         mysettings = getSharedPreferences("checkBox",0);
    }catch(NullPointerException e)
    {
        return false;
    }

    return mysettings.getBoolean("isChecked", false);
}

goes to Activity 1. Albo, make "checkBox" a constant value (public static final String).

Try now.

Upvotes: 2

Unii
Unii

Reputation: 1617

You should do the same in other activities (open shared preferences) and check if your checkbox is setted or not:

settings = Activity.this.getSharedPreferences("checkBox",0);
Boolean a = settings.getBoolean("isChecked", false);

Upvotes: 1

Related Questions