0

Have a good day. I want to set up shared preference activity.I have desined a dialog activity but if i save pref there can i take the pref from an other activity of my app.

@Override
public void pref(){

    SharedPreferences sharedpref =getSharedPreferences("BOYUT",Context.MODE_APPEND);
                        sharedpref=getSharedPreferences("THEME",Context.MODE_APPEND);
    SharedPreferences.Editor editor= sharedpref.edit();
     editor.putInt("BOYUT", sonboyut);
     editor.putInt("THEME",tema);
     editor.apply();
    Toast.makeText(getApplicationContext(),"Kaydedildi",Toast.LENGTH_LONG).show();

}

1 Answers1

0

getSharedPreferences and getDefaultSharedPreferences are like a file open commands. and apply() is like the close command. The first occurence of BOYUT here is used as a filename for a preference file, but a few lines down, you use use it as the name of a value you want to store.

I presume what you want is, to open the default SharedPreference file with getDefaultSharedPreferences, put the two values BOYUT and THEME into it, and apply() it. Then you can access these values via getDefaultSharedPreferences and getInt from other parts of your program.

What you DID was to create TWO preference files named BOYUT and THEME and put the values also named BOYUT and THEME into the later, leaving the first file emtpy. I'm not sure which one of the two you opened later to retrieve the values.

You might want to check this.

Community
  • 1
  • 1
karamike
  • 31
  • 2