5 Şubat 2018 Pazartesi

SharedPreferences.Editor Sınıfı

constructor
Şöyle yaparız.
SharedPreferences pref = ...;
SharedPreferences.Editor editor = pref.edit();
apply metodu - Yazma
Asenkron olarak yazar. Açıklaması şöyle.
commit() and .apply() has a minor difference .commits() return the boolean either it is success or not and .apply() returns nothing.
Örnek
Şöyle yaparız.
editor.apply(); // veya editor.commit()
Örnek
Şöyle yaparız.
public void saveData(String key,String data) {
  SharedPreferences preferences = this.getActivity().getSharedPreferences("Saved_data",
    Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = preferences.edit();
  editor.putString(key, data).apply();
}
clear metodu
Şöyle yaparız.
editor.clear();
commit  metodu- Yazma
Senkron olarak yazar. Şöyle yaparız.
editor.commit();
putBoolean metodu
Şöyle yaparız.
String keyName = ...;
boolean value = ...;
editor.putBoolean(keyName,value);
Şöyle yaparız.
editor.putBoolean("isLogin", true);
putInt metodu
Şöyle yaparız.
String keyName = ...;
int value = ...;editor.putInt(keyName, value);
putString metodu
Şöyle yaparız.
editor.putString("prefKey", myTextToEdit);

Hiç yorum yok:

Yorum Gönder