-3

I am trying to get a saved value from an activity by this Settings.getMode(); Here is the Settings class :

public class Set extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

private static String isNightMode;
Switch aSwitch;
Boolean mode;

@Override
protected void onCreate(Bundle savedInstanceState) {

    SharedPreferences sharedPreferences = getSharedPreferences("xyz", MODE_PRIVATE);
    isNightMode = sharedPreferences.getString("isNightMode", "no");
    mode = sharedPreferences.getBoolean("mode", false);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set);

    aSwitch = (Switch) findViewById(R.id.switch1);
    aSwitch.setChecked(mode);

    if (aSwitch.isChecked()) {
        isNightMode = "yes";
    } else {
        isNightMode = "no";
    }

    aSwitch.setOnCheckedChangeListener(this);
}

public static String getMode() {
    return isNightMode;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        isNightMode = "yes";
        mode = true;
        SharedPreferences.Editor editor = getSharedPreferences("xyz", MODE_PRIVATE).edit();
        editor.putString("isNightMode", isNightMode);
        editor.putBoolean("mode", mode);
        editor.commit();

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    } else {
        isNightMode = "no";
        mode = false;
        SharedPreferences.Editor editor = getSharedPreferences("xyz", MODE_PRIVATE).edit();
        editor.putString("isNightMode", isNightMode);
        editor.putBoolean("mode", mode);
        editor.commit();

        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}
}

When I change the value in Settings activity , the value is saved perfectly. But problem is , I am not getting the saved value when the app starts . When the app starts, the getMode() method returns null. There must be something wrong while getting saved value at the time of starting the app. I can't figure out the wrong.

Newaj
  • 3,992
  • 4
  • 32
  • 50
  • 1
    First of all, try to put `super.onCreate(savedInstanceState);` in your first line of `onCreate` method. And it would be great if you post your error log as well. – Mohammad Zarei Oct 28 '17 at 16:29
  • Possible duplicate of [Android Shared preferences example](https://stackoverflow.com/questions/23024831/android-shared-preferences-example) – Sushin Pv Oct 28 '17 at 16:37
  • @MohammadZarei, Actually there is no error . Problem is I am not getting the saved mode (day/night mode) when the app starts. – Newaj Oct 28 '17 at 16:40

2 Answers2

0

1.you can use it MyApplication .

2.and call MyApplication.getMode(); in your Activity .

Try this .

public class MyApplication extends Application {

private static String isNightMode;
Boolean mode;

@Override
public void onCreate() {
    super.onCreate();
    SharedPreferences sharedPreferences = getSharedPreferences("xyz", MODE_PRIVATE);
    isNightMode = sharedPreferences.getString("isNightMode", "no");
    mode = sharedPreferences.getBoolean("mode", false);
}

public static String getMode() {
    return isNightMode;
}

}

And add in the manifest

<application
    android:name=".yourPackage.MyApplication"
    ...                             />
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
0

This modification might help you. Modification in onCreate() method:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); // this - Activity

and

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit(); // this - Activity

    if (isChecked) {
        isNightMode = "yes";
        mode = true;                       

    } else {
        isNightMode = "no";
        mode = false;
    }

    editor.putString("isNightMode", isNightMode);
    editor.putBoolean("mode", mode);
    editor.apply();

    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}
Ibrokhim Kholmatov
  • 1,079
  • 2
  • 13
  • 16