Java tutorial
//package com.java2s; import android.content.Context; import android.content.SharedPreferences; public class Main { public static final String LOC1_KEY = "LOC1_KEY"; public static final String LOC2_KEY = "LOC2_KEY"; public static final String MOSTRECENT_LOCATIONSEARCH_SHARED_PREF = "LOCATION_KEY"; public static void writeCachedLocationSearchData(Context context, String recentData) { SharedPreferences cachePref = null; cachePref = context.getSharedPreferences(MOSTRECENT_LOCATIONSEARCH_SHARED_PREF, Context.MODE_PRIVATE); SharedPreferences.Editor editor = cachePref.edit(); if (cachePref.getString(LOC1_KEY, null) == null) { editor.putString(LOC1_KEY, recentData); } else { if (!cachePref.getString(LOC1_KEY, null).equalsIgnoreCase(recentData)) { editor.putString(LOC2_KEY, cachePref.getString(LOC1_KEY, "")); editor.putString(LOC1_KEY, recentData); } } editor.apply(); } }