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);
}
}