Skip to content Skip to sidebar Skip to footer

Saving Bundle Object Into Shared Preference

I have 2 Activities Activity1 Activity2 Activity1: Intent intent= new Intent(Activity1.this,Acivity2.class); Bundle b=new Bundle(); b.putParcelableArrayList('actionArray', (A

Solution 1:

in your Activity2

publicvoidaddTask(Task t) {
    if (null == currentTasks) {
        currentTasks = newArrayList<task>();
    }
    currentTasks.add(t);

    //save the task list to preferenceSharedPreferencesprefs= getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
    Editoreditor= prefs.edit();
    try {
        editor.putString(TASKS, ObjectSerializer.serialize(currentTasks));
    } catch (IOException e) {
        e.printStackTrace();
    }
    editor.commit();
}

Similarly we have to retrieve the list of tasks from the preference in the onCreate() method:

publicvoidonCreate() {
    super.onCreate();
    if (null == currentTasks) {
        currentTasks = newArrayList<task>();
    }

    //      load tasks from preferenceSharedPreferencesprefs= getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);

    try {
        currentTasks = (ArrayList<task>) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(newArrayList<task>())));
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

You can get ObjectSerializer class from Apache Pig project ObjectSerializer.java

Solution 2:

preferences = mActivity.getSharedPreferences(
                    SharePreference_demo, Context.MODE_PRIVATE); 



 Map<String, String> demoarraylist= newHashMap<String, String>();
    demoarraylist.put("actionArray", "action");
    demoarraylist.put("comedyArray", "comedy");
    demoarraylist.put("lovearray", "love");
    demoarraylist.put("classicArray", "classic");
    demoarraylist.put("familyArray", "family");



SharedPreferences.Editor editor = preferences.edit();
                for (Entry<String, String> entry : demoarraylist.entrySet()) {
                    editor.putString(entry.getKey(), entry.getValue());
                }
             editor.commit();

put above code put in your 1st activity.

and below code is put in your 2nd activity.

 Map<String, String> demoarraylist= new HashMap<String, String>();


 SharedPreferences preferences = activity.getSharedPreferences(
                    SharePreference_demo, Context.MODE_PRIVATE);
            for (Entry<String, ?> entry : preferences.getAll().entrySet()) {
                demoarraylist.put(entry.getKey(), entry.getValue().toString());
            }


Iterator myVeryOwnIterator = demoarraylist.keySet().iterator();
        while (myVeryOwnIterator.hasNext()) {
            String key = (String) myVeryOwnIterator.next();
            String value = (String) MapListUserStatus.get(key);
}

Solution 3:

Map<String, String> MapListUserStatus = newHashMap<String, String>();

    SharedPreferences.Editor editor = preferences.edit();
                for (Entry<String, String> entry : MapListUserStatus.entrySet()) {
                    editor.putString(entry.getKey(), entry.getValue());
                }

Store your ArrayListData into sharedprefernces using HashMap().

Retrieve sharedPreferences data:

MapListUserStatus = new HashMap<String, String>();




 SharedPreferences preferences = conductpd.getSharedPreferences(
                    SharePreference_messages_history, Context.MODE_PRIVATE);
            for (Entry<String, ?> entry : preferences.getAll().entrySet()) {
                MapListUserStatus.put(entry.getKey(), entry.getValue().toString());
            }

I hope its useful to you..if any query.please tell me.

Post a Comment for "Saving Bundle Object Into Shared Preference"