Breaking

Post Top Ad

Your Ad Spot

Wednesday, 8 May 2019

Shared Preferences

Shared Preferences

--------------------------------------------------------Code-----------------------------------------------------------
package com.example.anwar.politix.accessories;

import android.content.Context;
import android.content.SharedPreferences;

import static android.content.Context.MODE_PRIVATE;

public class SharedPreference {

    public static String getPreference(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences("PREF", MODE_PRIVATE);
        //String data = preferences.getString(key, null);
        return preferences.getString(key, null);
    }

    public static void setPreference(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences("PREF", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static void removePrefrence(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences("PREF", MODE_PRIVATE);
        preferences.edit().remove(key).apply();
    }

}

No comments:

Post a Comment