Android 离开首选项活动,调用活动仍读取旧首选项?

Android 离开首选项活动,调用活动仍读取旧首选项?,android,sharedpreferences,Android,Sharedpreferences,我觉得我遗漏了一些明显的东西,但搜索把我带到了几个不同的点击,所有这些都不能直接访问我的古怪问题 拥有一个包含主活动和首选活动的应用程序。添加一个“preference”类,简化阅读和设置首选项。主活动有一个选项菜单,用于访问首选项活动: //Try reloading preferences? SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); String test = sp.getStrin

我觉得我遗漏了一些明显的东西,但搜索把我带到了几个不同的点击,所有这些都不能直接访问我的古怪问题

拥有一个包含主活动和首选活动的应用程序。添加一个“preference”类,简化阅读和设置首选项。主活动有一个选项菜单,用于访问首选项活动:

//Try reloading preferences?
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String test = sp.getString(Preferences.OPTION_FILTERSTATIONTYPE, "---");
Log.e("BMMaps", test);
Preferences类(包括相关性,如果我不使用该类阅读设置,也会发生同样的情况)

优先活动:

public class AppSettings extends PreferenceActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.app_preferences);

        ListPreference stationType = (ListPreference)this.findPreference(this.getString(R.string.option_filterStationType));
        stationType.setOnPreferenceChangeListener(this.stationOrderEnable());
    }
[...]
}
最后两行连接一个事件,以根据自己的选择启用/禁用其他首选项。正如预期的那样,这是可行的。 简单的主要活动和相关功能:

public class MainMapScreen extends MapActivity
{
    private void launchSettings()
    {
        Intent prefsIntent = new Intent(this.getApplicationContext(), AppSettings.class);
        this.startActivity(prefsIntent);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        Preferences.getMapShowSatellite(); // <-- Returns previous value.
        // Re-start the MyLocation Layer from tracking.
        this._mapView.requestLayout();
    }
[...]
}
此时从离开PreferenceActivity开始记录的是旧设置。手动读取首选项文件会显示.xml文件正在使用用户的新设置进行更新

因为这并不明显,我被谷歌的地图API吸引住了。因此,我必须指定两个不同的流程——一个用于主要活动(这一个),另一个用于与此问题无关的活动。所有其他活动,包括PreferencesActivity,在其定义中没有指定的
android:process=“”

编辑3: 根据要求,以下是数据首选项文件:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name="com.tsqmadness.bmmaps.filterStationType">V-?</string>
    <boolean name="com.tsqmadness.bmmaps.deviceHasLocation" value="false" />
</map>

V-?
下面是首选存储XML文件:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Map Options">
        <CheckBoxPreference android:key="com.tsqmadness.bmmaps.mapShowSatellite" android:order="1" android:summary="Whether or not to show satellite imagery on the map." android:summaryOff="Standard road map will be shown." android:summaryOn="Satellite imagery will be show." android:title="Show Satellite Layer?" />
        <CheckBoxPreference android:key="com.tsqmadness.bmmaps.mapShowScale" android:order="2" android:summary="Whether or not to show the distance bar on the map." android:summaryOff="The distance bar will not be shown on the map." android:summaryOn="The distance bar will be shown on the map." android:title="Show Map Scale?" />
        <CheckBoxPreference android:defaultValue="false" android:key="com.tsqmadness.bmmaps.useMetric" android:order="3" android:summary="Whether to use Metric os SI values." android:summaryOff="SI units (mi/ft) will be shown." android:summaryOn="Metric units (km/m) will be shown." android:title="Use Metric?" />
        <ListPreference android:dialogTitle="Station Load Delay" android:entries="@array/static_listDelayDisplay" android:entryValues="@array/static_listDelayValues" android:key="com.tsqmadness.bmmaps.mapBMDelay" android:negativeButtonText="Cancel" android:order="4" android:positiveButtonText="Save" android:summary="The delay after map panning before staions are loaded." android:title="Delay Before Loading" />
    </PreferenceCategory>
    <PreferenceCategory android:title="Control Station Filter">
        <ListPreference android:dialogTitle="Station Type" android:entries="@array/static_listStationTypeDisplay" android:entryValues="@array/static_listStationTypeValues" android:key="com.tsqmadness.bmmaps.filterStationType" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="The station type to filter on." android:title="Station Type" android:order="1" />
        <ListPreference android:dialogTitle="Select Station Order" android:entries="@array/static_listStationHOrderDisplay" android:entryValues="@array/static_listStationHOrderValues" android:key="com.tsqmadness.bmmaps.filterStationOrder" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="Station Order to filter by." android:title="Station Order" android:order="2" />
        <ListPreference android:dialogTitle="Select Station Stability" android:entries="@array/static_listStationStabilityDisplay" android:entryValues="@array/static_listStationStabilityValues" android:key="com.tsqmadness.bmmaps.filterStationStability" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="Station stability to filter by." android:title="Station Stability" android:order="3" />
        <CheckBoxPreference android:key="com.tsqmadness.bmmaps.filterNonPub" android:summaryOff="Non-Publishable stations will not be shown." android:defaultValue="false" android:summaryOn="Non-Publishable stations will be shown on the map." android:order="4" android:title="Show Non-Publishable" />
    </PreferenceCategory>
    <Preference android:key="temp" android:title="Test" android:summary="Test Item">
        <intent android:targetClass="com.tsqmadness.bmmaps.activities.MainMapScreen" android:targetPackage="com.tsqmadness.bmmaps" />
    </Preference>
</PreferenceScreen>

当更改
filterStationType
参数并点击PreferenceActivity的后退按钮时,preferences文件将从上面的
V-?
更改为
H-?
,这是应该的。但是,在应用程序重新启动之前,从主活动的SharedReferences中读取值仍然会给出
V-?
。啊,另外,我在PreferenceActivity中有一个
onPreferenceChangeListener()
,当值更改时调用它


最终编辑:显然,这是对给定活动使用命名的android:process。Google maps API需要这样做,才能允许同一应用程序中的两个单独的MapActivity使用不同的设置。如果PreferenceActivity被移动到同一个命名的进程,那么上面的代码读取
onResume()
中的设置将返回正确的值。

在Resume上,您必须获取对SharedReferences的新引用,否则您只是在使用内存中已经存在的对象(并且具有旧值)

编辑: 与其否决你不理解的答案,为什么不要求澄清呢? 我的意思是,你应该以正确的方式(而不是你是如何做的)获取你的pref


然后看看会发生什么。

既然一切正常,我猜您的首选项活动的
app\u preferences.xml
文件中的key
R.string.option\u showstartellite
(无论该字符串是什么)有一个输入错误,或者不正确或未定义。这将导致两个键[unbeknownst to you]具有不同的名称,您认为它们指向相同的值。实际上,prefs活动使用一个键,而您的首选项类使用另一个键——导致两个不同的键和两个不同的值

仔细检查你的钥匙。确保您没有使用literal
“R.string.options\u showstartellite”
作为xml文件中的键,而是实际的字符串。如果您想使用本地化版本,则
@string/options\u showstartellite
适用于密钥。但是,键不需要本地化


如果您感到好奇,可以通过在标准文本编辑器中打开由应用程序数据目录中的首选项管理器创建的首选项文件来仔细检查这一点。

仍在查看您的代码,但这里有一个更好的代码提示:对设置键使用
静态最终字符串
s而不是字符串资源。这些值不需要本地化,实际上永远不应该更改。这些和一些小的性能原因使它们成为常数的好选择。当您在您的
showstartellite
首选项上添加
onpreferencechangeelistener
时,您会看到什么样的行为?@austin-谢谢!我是Java新手,所以我确信我的代码在技术上很糟糕。:)我将研究附加一个侦听器,明天回来更新..另一个提示,与其每次都使用Preferences类的静态方法并传递您的上下文,为什么不保留对PreferenceManager的引用呢?如果您要对静态首选项方法进行多个调用?不会改变任何东西,我试着做了一个
PreferenceManager.getDefaultSharedReferences(这个)在获得设置之前,它不会改变我得到的答案。不熟悉Java并且不知道自己在做什么并不是拒绝投票给试图帮助你的人的(正确)答案的最佳理由。@Dr.Dredel我认为你有打字错误。你的意思是“试图帮助你的人的回答不正确”,对吗?(;@David,你是说我建议OP检索他的共享首选项的方式有什么不正确吗?@Dr.Dredel好吧,现在没有什么不好的感觉了。让我试着解释一下。你使用
SharedReferences
的方法建议OP在他的
上获取一个新的
SharedReference
s的引用Resume
方法。这只需要在
onCreate
方法中发生。这是因为
SharedReference
只是一个“指针”添加到活动使用的首选项文件。此文件在同一应用程序中跨活动共享,并且
SharedReference
对象本身中从未存储任何信息。但是,您确实需要重新加载活动可能已加载的
onResume
首选项字段。以上所有内容均已签出,oddl
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Map Options">
        <CheckBoxPreference android:key="com.tsqmadness.bmmaps.mapShowSatellite" android:order="1" android:summary="Whether or not to show satellite imagery on the map." android:summaryOff="Standard road map will be shown." android:summaryOn="Satellite imagery will be show." android:title="Show Satellite Layer?" />
        <CheckBoxPreference android:key="com.tsqmadness.bmmaps.mapShowScale" android:order="2" android:summary="Whether or not to show the distance bar on the map." android:summaryOff="The distance bar will not be shown on the map." android:summaryOn="The distance bar will be shown on the map." android:title="Show Map Scale?" />
        <CheckBoxPreference android:defaultValue="false" android:key="com.tsqmadness.bmmaps.useMetric" android:order="3" android:summary="Whether to use Metric os SI values." android:summaryOff="SI units (mi/ft) will be shown." android:summaryOn="Metric units (km/m) will be shown." android:title="Use Metric?" />
        <ListPreference android:dialogTitle="Station Load Delay" android:entries="@array/static_listDelayDisplay" android:entryValues="@array/static_listDelayValues" android:key="com.tsqmadness.bmmaps.mapBMDelay" android:negativeButtonText="Cancel" android:order="4" android:positiveButtonText="Save" android:summary="The delay after map panning before staions are loaded." android:title="Delay Before Loading" />
    </PreferenceCategory>
    <PreferenceCategory android:title="Control Station Filter">
        <ListPreference android:dialogTitle="Station Type" android:entries="@array/static_listStationTypeDisplay" android:entryValues="@array/static_listStationTypeValues" android:key="com.tsqmadness.bmmaps.filterStationType" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="The station type to filter on." android:title="Station Type" android:order="1" />
        <ListPreference android:dialogTitle="Select Station Order" android:entries="@array/static_listStationHOrderDisplay" android:entryValues="@array/static_listStationHOrderValues" android:key="com.tsqmadness.bmmaps.filterStationOrder" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="Station Order to filter by." android:title="Station Order" android:order="2" />
        <ListPreference android:dialogTitle="Select Station Stability" android:entries="@array/static_listStationStabilityDisplay" android:entryValues="@array/static_listStationStabilityValues" android:key="com.tsqmadness.bmmaps.filterStationStability" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="Station stability to filter by." android:title="Station Stability" android:order="3" />
        <CheckBoxPreference android:key="com.tsqmadness.bmmaps.filterNonPub" android:summaryOff="Non-Publishable stations will not be shown." android:defaultValue="false" android:summaryOn="Non-Publishable stations will be shown on the map." android:order="4" android:title="Show Non-Publishable" />
    </PreferenceCategory>
    <Preference android:key="temp" android:title="Test" android:summary="Test Item">
        <intent android:targetClass="com.tsqmadness.bmmaps.activities.MainMapScreen" android:targetPackage="com.tsqmadness.bmmaps" />
    </Preference>
</PreferenceScreen>
 SharedPreferences sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);