0

I'm using SharedPreferences to create settings on one activity that will be applied on other activities. I've created the SharedPreferences in OnCreate, but then I need to set them in a second function that is called when a button is pressed. At the moment the app keeps crashing on launch if I put SharedPreferences anywhere except for OnCreate.

Problem is I don't seem to be able to carry sharedPreferences into the openNextPage function, as all mentions of it in openNextPage bring up an error saying :

Cannot resolve symbol sharedPreferences

So how can I carry the editor over to this function?

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

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    Button nextBtn = findViewById(R.id.confSetBtn);
    nextBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openNextPage();
        }
    });
}

public void openNextPage(){
    SharedPreferences.Editor editor = sharedPreferences.edit();

    if(checkBox1.isChecked()){
        editor.putBoolean("value1", true);
    }
    if(checkBox2.isChecked()){
        editor.putBoolean("value2", true);
    }
    editor.apply();

    boolean none = sharedPreferences.getBoolean("value1", false);

    if(none){
        finish();
    }
    else{
        Intent intent = new Intent(welcomeScreen.this, newSettingsActivity.class);
        startActivity(intent);
    }
}
Edric
  • 24,639
  • 13
  • 81
  • 91
MendelumS
  • 91
  • 1
  • 8
  • I think you can declare SharedPrefernces as a field of this class – Humza Malik Jan 05 '20 at 14:21
  • @HumzaMalik how would I do that? Still kind of new to android studio – MendelumS Jan 05 '20 at 14:23
  • you need to learn java first ... You can declare this variable as a field and initiate in onCreate method.......... if you want to use in another method you can also pass sharedpreferences to that method as reference – Humza Malik Jan 05 '20 at 14:24

3 Answers3

1

You can try it like this i have edited your code. I have declared this as property in your class.

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

            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

            Button nextBtn = findViewById(R.id.confSetBtn);
            nextBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openNextPage();
                }
            });
    }

    public void openNextPage(){
            SharedPreferences.Editor editor = sharedPreferences.edit();

            if(checkBox1.isChecked()){
                editor.putBoolean("value1", true);
            }
            if(checkBox2.isChecked()){
                editor.putBoolean("value2", true);
            }
            editor.apply();

            boolean none = sharedPreferences.getBoolean("value1", false);

            if(none){
                finish();
            }
            else{
                Intent intent = new Intent(welcomeScreen.this, newSettingsActivity.class);
                startActivity(intent);
            }
        }
Humza Malik
  • 343
  • 3
  • 14
  • This solves the issue of the values not passing over to ```openNextPage``` but the app is still crashing on launch. Any ideas why? – MendelumS Jan 05 '20 at 14:45
  • @Sam that might be fault in your application code ... Review what you exactly want to do then implement that – Humza Malik Jan 05 '20 at 15:10
0

To access a variable across app declare it as:

public static SharedPreferences object;

Follow this link to understand how to use shared preferences in your app. Link

Cheers!

0

Pass instance of SharedPreferences as an argument as follows

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

    // update 1
    final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 

    Button nextBtn = findViewById(R.id.confSetBtn);
    nextBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // update 2
            openNextPage(sharedPreferences);  
        }
    });
}

// update 3
public void openNextPage(SharedPreferences sharedPreferences){  
    SharedPreferences.Editor editor = sharedPreferences.edit();

    if(checkBox1.isChecked()){
        editor.putBoolean("value1", true);
    }
    if(checkBox2.isChecked()){
        editor.putBoolean("value2", true);
    }
    editor.apply();

    boolean none = sharedPreferences.getBoolean("value1", false);

    if(none){
        finish();
    }
    else{
        Intent intent = new Intent(welcomeScreen.this, newSettingsActivity.class);
        startActivity(intent);
    }
}