简体   繁体   中英

Android Studio SharedPreferences of boolean and if conditions

Task: - When a save button is clicked: Save a checkbox's boolean value to SharedPreferences. - On App boot check if the SharedPreferences value is true, if so check the checkbox.

Not sure in which part of the above i'm wrong, so i present you the whole thing. (Searched a lot the web and trying to solve this for days, excuse y newbie-city)

MainActivity.java

public class MainActivity extends AppCompatActivity {
    boolean ever_saved_settings=false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String if_settings= String.valueOf(ever_saved_settings);
    if(if_settings.equals("true")) {
        SharedPreferences Saved_Settings = getPreferences(MODE_PRIVATE);
        String PU2 = Saved_Settings.getString("PU", null);
        if (PU2=="true")
            chk_box_PU.setChecked(true);
            else
            chk_box_PU.setChecked(false);

    }

public void save_settings(View v) {
    ever_saved_settings=true;
    CheckBox chk_box_PU = (CheckBox) findViewById(R.id.chk_box_PU);
    String PU_value = (String) chk_box_PU.getText();
    SharedPreferences Saved_Settings = this.getSharedPreferences("Saved_Settings", MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = Saved_Settings.edit();
    prefsEditor.putString("PU", PU_value);
    prefsEditor.commit();
}
}

EDIT

Here arethe /xml's just in case the problem is not in the activity. Rare but possible for a newbie :}

toolbar_save_button.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:orientation="horizontal">


<Button
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:text="@string/save_button"
    android:textSize="24dp"
    android:onClick="save_settings"/>

which is included in: home.xml

<merge
xmlns:android="http://schemas.android.com/apk/res/android">
[...]
<include layout="@layout/toolbar_save_button"/>
</merge>

whish is included in: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
android:gravity="center_horizontal"
android:background="@drawable/back_0"
tools:context=".MainActivity">
[...]
<include layout="@layout/home"/>
[...]
</LinearLayout>

The button looks just fine running the .apk in an android cell phone, it "blibs" when you click it.

Try this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    SharedPreferences Saved_Settings = getPreferences("Saved_Settings", MODE_PRIVATE);
    boolean PU2 = Saved_Settings.getBoolean("PU", false);

    // no need to check with if conditions. just set the button with the value(PU2)
    chk_box_PU.setChecked(PU2);

}

public void save_settings(View v) {
    CheckBox chk_box_PU = (CheckBox) findViewById(R.id.chk_box_PU);
    SharedPreferences Saved_Settings = this.getSharedPreferences("Saved_Settings", MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = Saved_Settings.edit();
    prefsEditor.putBoolean("PU", chk_box_PU.isChecked());
    prefsEditor.commit();
}

1. You don't need this variable:

boolean ever_saved_settings=false;

This variable is set to false every time you launch your application. Also you will not get any error if settings don't exist, so the variable is useless.


2. Your line

if (PU2=="true")

will always return false. In this line you are comparing pointers, not values.

Always use it in this way:

if (PU2.equals("true"))


3. Why do you store boolean values in a String, and compare them as Strings?

You can compare boolean variables, for example:

boolean variable = true;
if (variable) {
    // do your stuff
}


4. here is how you should implement it:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SharedPreferences Saved_Settings = getSharedPreferences("Saved_Settings", MODE_PRIVATE);
    chk_box_PU.setChecked(Saved_Settings.getBoolean("PU", false));
}

public void save_settings(View v) {
    CheckBox chk_box_PU = (CheckBox) findViewById(R.id.chk_box_PU);

    SharedPreferences Saved_Settings = getSharedPreferences("Saved_Settings", MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = Saved_Settings.edit();

    prefsEditor.putBoolean("PU", chk_box_PU.isChecked());

    prefsEditor.apply();
}}

I think that you are missing the preference name:

   SharedPreferences Saved_Settings = getPreferences(MODE_PRIVATE);

Change to:

   SharedPreferences Saved_Settings = getPreferences("Saved_Settings", MODE_PRIVATE);

in your onCreate() change this

SharedPreferences Saved_Settings = getPreferences(MODE_PRIVATE);

to

SharedPreferences Saved_Settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

and this if (PU2=="true") to if(PU2.equals("true"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM