0

I have a simple problem. I cannot find a way to use sharedPreferences between my Activities. One of them is a Settingsactivity and the other one is my MainActivity. I would like to save variables (show_idle_dialog and selected_currency) in the SettingsActivity to sharedPreferences and load it in the MainActivity. With my current code the MainActivity always loads the defValue.

SettingsActivity:

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

public class SettingsActivity extends AppCompatActivity {

    public static final String KEY_PREF_CHECK_BOX_SHOW_IDLE_DIALOG = "check_box_show_idle_dialog";
    public static final String KEY_PREF_CURRENCY_DROPDOWN = "currency_dropdown";

    Boolean show_idle_dialog;
    String selected_currency;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        show_idle_dialog = sharedPreferences.getBoolean(SettingsActivity.KEY_PREF_CHECK_BOX_SHOW_IDLE_DIALOG, true);
        selected_currency = sharedPreferences.getString(SettingsActivity.KEY_PREF_CURRENCY_DROPDOWN, "currency_eur");

        Log.i("abc", "put " + show_idle_dialog);
        Log.i("abc", "put " + selected_currency);
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {

    public static final String SHARED_PREFS = "sharedPrefs";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        PreferenceManager.setDefaultValues(this, R.xml.root_preferences, false);
        
       }
       
       @Override
    public void onResume(){
        loadData();
        super.onResume();
    }
    
    public void loadData(){

        SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);

        show_idle_dialog  = sharedPreferences.getBoolean(SettingsActivity.KEY_PREF_CHECK_BOX_SHOW_IDLE_DIALOG, false);
        selected_currency = sharedPreferences.getString(SettingsActivity.KEY_PREF_CURRENCY_DROPDOWN, "currency_eur");

        Log.i("abc", "got " + show_idle_dialog);
        Log.i("abc", "got " + selected_currency);

    }

I would be very happy if someone knows what I've done wrong.

BlazeCodeDev
  • 631
  • 1
  • 7
  • 27
  • 1
    In the settings activity you have `SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)` but in the other activity `SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);`. That is wrong of course. You should have used the same. – blackapps Dec 18 '19 at 23:05
  • In the settings activity you dont have to save yourself. – blackapps Dec 18 '19 at 23:07

1 Answers1

1

You are trying to get value from shared preferences in the SettingsActivity.

To save value in shared preferences you can do:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

     // Writing data to SharedPreferences
     Editor editor = settings.edit();
     editor.putString("key", "some value");
     editor.commit();

So what you need to do in SettingsActivity is:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

     // Writing data to SharedPreferences
     Editor editor = sharedPreferences .edit();
     editor.putString(SettingsActivity.KEY_PREF_CURRENCY_DROPDOWN, <your-string-value>);
     editor.putBoolean(SettingsActivity.KEY_PREF_CHECK_BOX_SHOW_IDLE_DIALOG, <your-boolean-value>)
     editor.commit();
Saugat Jonchhen
  • 356
  • 5
  • 16
  • Thank you! How do I know the value which I have to pass to the editor? The 2 lines in the SettingsActivity are the only way to read the current state which I know. – BlazeCodeDev Dec 18 '19 at 19:46
  • These values are the ones you want to save in the shared prefs. Also please give an accepted answer and upvote. Happy coding! – Saugat Jonchhen Dec 19 '19 at 01:08
  • I know that. My problem is I don't know how to read the current value from the checkBox. The only way I know is by reading what the SettingsActivity has written to SharedPreferences. – BlazeCodeDev Dec 19 '19 at 06:35
  • To get the current value from checkbox you need to `setOnCheckChangedListener()`. This [link](https://stackoverflow.com/questions/8386832/android-checkbox-listener) might help you – Saugat Jonchhen Dec 19 '19 at 06:37
  • I tried that, and it turns out that won't work because it's not a normal checkBox, it's a checkBoxPreference and that doesn't have a onCheckChangedListener. Every method which I tried involves preference.find, which is depricated. – BlazeCodeDev Dec 19 '19 at 06:45