Android SharedPreferences Save putPrefs(Activity context, int mode, Map pairs)

Here you can find the source of putPrefs(Activity context, int mode, Map pairs)

Description

Purpose - Put preferences through the Shared Preferences API.

Parameter

Parameter Description
context - Activity context
mode - Mode to open shared preferences
pairs - map to put in shared preferences

Declaration

public static void putPrefs(Activity context, int mode,
        Map<String, String> pairs) 

Method Source Code

//package com.java2s;
import java.util.Map;
import java.util.Map.Entry;
import android.app.Activity;

import android.content.SharedPreferences;

public class Main {
    /**/*from ww  w  . java2s  .  c  om*/
     * Purpose - Put preferences through the Shared Preferences API. 
     * Use this from an activity if you need to use only one shared preference file for the activity
     * 
     * @param context - Activity context
     * @param mode - Mode to open shared preferences
     * @param pairs - map to put in shared preferences
     * */
    public static void putPrefs(Activity context, int mode,
            Map<String, String> pairs) {
        SharedPreferences prefs = context.getPreferences(mode);
        if (pairs != null && !pairs.isEmpty()) {
            SharedPreferences.Editor editor = prefs.edit();
            for (Entry<String, String> pair : pairs.entrySet()) {
                editor.putString(pair.getKey(), pair.getValue());
            }
            editor.commit();
        }
    }
}

Related

  1. putSharedPrefs(Context context, String prefName, int mode, Map pairs)
  2. saveSetting(Context context, String name, Long value)
  3. saveSetting(Context context, String name, String value)
  4. updatePrefValueByModel(SharedPreferences pref, String key, String thisModel, String targetModel, Object targetModelValue, Object notTargetModelValue)