I have a TextPreference
in my settings activity, and I would like to get the changing value from that from another activity.
The startActivityForResult()
method can help ?
preference xml :
<EditTextPreference
android:key="@string/pref_location_key"
android:title="@string/pref_location_label"
android:defaultValue="@string/pref_location_default"
android:inputType="text"
android:singleLine="true" />
I usually use a preference utility class as below and then import the static into the Activity or Fragment as needed. Here's your example as applied to a preference class used in Google's iosched 2014 app.
/*
* Copyright 2014 Google Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package com.your.package;
/** * Utilities and constants related to app preferences. */ public class PrefUtils {
public static final String pref_location_key = "pref_location_key";
public static String getPrefLocationKey(final Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
return sp.getString(pref_location_key, "map");
}
public static void setPrefLocationKey(final Context context, String _pref_location_key) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
sp.edit().putString(pref_location_key, _pref_location_key).apply();
}
}
The above will link and act upon your value in your preference.xml file as long as the String pref_location_key = "pref_location_key";
value matches that in your preference file.
Note that you can change the .apply() to .commit() dependant how fast or aggressively you want to access the value, but be aware that you may also have to resolve an immediate access to the value may be better by either passing it to the next intent if needed immediately or you could also use third party library EventBus EventBus linkto pass it to other Activities or Fragments if you want to trigger a action/event along with that value being set and looser coupling.
So in your Activity you could then call either getPrefLocationKey(getBaseContext())
or setPrefLocationKey(getBaseContext(), "Your desired value to set the string to.")
In a Fragment the context would be prefaced by the activity, since a fragment doesn't have a context. getPrefLocationKey(getActivity().getBaseContext()) ` or `setPrefLocationKey(getActivity().getBaseContext(), "Your desired value to set the string to.")
In most or many case in a Activity the getBaseContext()
can be replaced by this
.
In all cases the setPrefLocationKey or getPrefLocationKey should be a static import into your Activity or Fragment.