-1

I want to use sharedpreferrences in android. I put data to it but when get data in other activity, it can not find prefs.getboolean.

class A{
   SharedPreferences.Editor editor = ApplicationLoader.applicationContext.getSharedPreferences("keymode", 0).edit();
   editor.putBoolean("BotKey", true);
   editor.commit();}
class B{
  SharedPreferences prefs = ApplicationLoader.applicationContext.getSharedPreferences("keymode", 0);
  SharedPreferences.Editor editor = prefs.edit();
  if(prefs.getBoolean("BotKey",false)){}
  }
Tom
  • 19
  • 1
  • 8

1 Answers1

0

Try to use that way:

To store values in shared preferences:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("BotKey", true);
editor.apply();

To retrieve values from shared preferences:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean isBotKey = preferences.getBoolean("BotKey", false);

Like Ziem said on: https://stackoverflow.com/a/11027631/5090511

Community
  • 1
  • 1
Pedro Fernandes
  • 336
  • 1
  • 3
  • 11