bfmv991
bfmv991

Reputation: 109

Get SharedPreference between two Activitys

I have troubles reading my SharedPreference from ContactView.Java in my MainActivity.Java

This is what i have in my ContactView.Java:

SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("contactName", name);
        editor.putString("contactPhone", phoneNo);
        editor.commit();

and this in my OnCreate for setting them to the TextViews:

SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
    String name = settings.getString("contactName", "");
    //the second parameter set a default data if “contactName” is empty
    if (!name.isEmpty()){
        textView1.setText(name);
    }
    String phoneNo = settings.getString("contactPhone", "");//the second parameter set a default data if “contactName” is empty
    if (!phoneNo.isEmpty()){
        textView2.setText(phoneNo);
    }

Now when i go to MainActivity i would like to read them:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String phoneNo =  sharedPreferences.getString("contactPhone", "");
String name =  sharedPreferences.getString("contactName", "");

But the Strings seem to be empty and not contain any name or phoneNo what am i doing wrong?

Upvotes: 0

Views: 74

Answers (4)

Sagar Zala
Sagar Zala

Reputation: 5144

1- You can create custom class for adding, retrieving, checking and deleting data in SharedPreference very easily.

public class Pref_Storage {
    private static SharedPreferences sharedPreferences = null;

    public static void openPref(Context context) {
        sharedPreferences = context.getSharedPreferences(context.getResources().getString(R.string.app_name),
                Context.MODE_PRIVATE);
    }

    public static void deleteKey(Context context, String key) {
        HashMap<String, String> result = new HashMap<String, String>();

        Pref_Storage.openPref(context);
        for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                .entrySet()) {
            result.put(entry.getKey(), (String) entry.getValue());
        }

        boolean b = result.containsKey(key);
        if (b) {
            Pref_Storage.openPref(context);
            Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
            prefsPrivateEditor.remove(key);

            prefsPrivateEditor.commit();
            prefsPrivateEditor = null;
            Pref_Storage.sharedPreferences = null;
        }
    }

    public static void setDetail(Context context, String key, String value) {
        Pref_Storage.openPref(context);
        Editor prefsPrivateEditor = Pref_Storage.sharedPreferences.edit();
        prefsPrivateEditor.putString(key, value);

        prefsPrivateEditor.commit();
        prefsPrivateEditor = null;
        Pref_Storage.sharedPreferences = null;
    }

    public static Boolean checkDetail(Context context, String key) {
        HashMap<String, String> result = new HashMap<String, String>();

        Pref_Storage.openPref(context);
        for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                .entrySet()) {
            result.put(entry.getKey(), (String) entry.getValue());
        }

        boolean b = result.containsKey(key);
        return b;
    }

    public static String getDetail(Context context, String key) {
        HashMap<String, String> result = new HashMap<String, String>();

        Pref_Storage.openPref(context);
        for (Entry<String, ?> entry : Pref_Storage.sharedPreferences.getAll()
                .entrySet()) {
            result.put(entry.getKey(), (String) entry.getValue());
        }

        String b = result.get(key);
        return b;

    }
}

For set data:

Pref_Storage.setDetail(context, "contactName", name);
Pref_Storage.setDetail(context, "phoneNo", phoneNo);

For retrieving data:

Pref_Storage.getDetail(context, "contactName");
Pref_Storage.getDetail(context, "phoneNo");

For checking data:

Pref_Storage.checkDetail(context, "contactName");
Pref_Storage.checkDetail(context, "phoneNo");

For deleting data:

Pref_Storage.deleteKey(context, "contactName");
Pref_Storage.deleteKey(context, "phoneNo");

Upvotes: 0

vishal jangid
vishal jangid

Reputation: 3025

In two activity A and B you can use shared preferences in this following manner.

in activity A you can create a shared preferences

SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString("contactName", name);
        editor.putString("contactPhone", phoneNo);
        editor.commit();

In activity B you can use the shared prefrences with same name

SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
 String phoneNo =  settings .getString("contactPhone", "");
String name =  settings .getString("contactName", "");

Upvotes: 1

issathink
issathink

Reputation: 1230

In your MainActivity you're supposed to use the same preference file:

SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
String phoneNo =  settings.getString("contactPhone", "");
String name =  settings.getString("contactName", "");

Upvotes: 2

mikeD
mikeD

Reputation: 481

You are accessing 2 different SharedPreferences files.

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

is different than

SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);

Use

PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

everywhere.

Upvotes: 3

Related Questions