1

明らかな何かが欠けているように感じますが、検索によっていくつかの異なるヒットが見つかりました。これらはすべて、私の奇妙な問題に直接アクセスしていません。

メインアクティビティと設定アクティビティを備えたアプリを用意します。これに「preference」クラスを追加します。これにより、設定の読み取りと設定が簡単になります。メインアクティビティには、設定アクティビティにアクセスするためのオプションメニューがあります。

環境設定クラス(関連性のために含まれています。このクラスを使用して設定を読み取らない場合も同じことが起こります)。

public class Preferences
{
    public static SharedPreferences getPrefs(Context context)
    {
        SharedPreferences retCont = PreferenceManager.getDefaultSharedPreferences(context);
        return retCont;
    }

    /* Map Page: Show Satellite */
    public static boolean getMapShowSatellite(Context context)
    {
        return Preferences.getPrefs(context).getBoolean(Preferences.getString(context, R.string.option_showSatellite), false);
    }

    public static void setMapShowSatellite(Context context, boolean newValue)
    {
        Editor prefsEditor = Preferences.getPrefs(context).edit();
        prefsEditor.putBoolean(Preferences.getString(context, R.string.option_showSatellite), newValue);
        prefsEditor.commit();
    }
}

PreferencesActivity:

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());
    }
[...]
}

最後の2行はイベントをフックして、選択に基づいて他の設定を有効/無効にします。予想通り、それは機能します。単純な主なアクティビティと関連する機能:

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();
    }
[...]
}

さて、何が起こるかというと、アプリを実行するとしましょう。アプリの読み込み時に、がgetMapShowSatellite()返されますTrue。PreferenceActivityに移動し、そのオプションをに変更しますFalse。[戻る]ボタンを押して、PreferenceActivityを終了します。このとき、メインアクティビティonResume()はと呼ばれます。getMapShowSatellite()この時点でを取得すると、以前の設定のが返されますTrue。アプリを終了して再起動すると、最終的にFalse期待どおりの状態に戻ります。

私は.commit()手動で電話をかけていません-そして私がそうする必要はないと思います、設定が保存されていることを確認してください、私はただ更新値を取得していません。

何が足りないの?:) - 狐。

編集2:小さな更新。問題は静的呼び出しである可能性があると思ったので、一時的に、Preferencesクラス(上記)を、静的ではなくインスタンス化されたクラスに変更しました。onResume()また、メインアクティビティの呼び出しに次のコードを追加しました。

//Try reloading preferences?
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String test = sp.getString(Preferences.OPTION_FILTERSTATIONTYPE, "---");
Log.e("BMMaps", test);

PreferenceActivityを離れてからこの時点でログに記録されるのは、古い設定です。設定ファイルを手動で読み取ると、.xmlファイルがユーザーの新しい設定で更新されていることがわかります。

はっきりしないので、私はGoogleのMapsAPIに夢中になっています。このため、2つの異なるプロセスを指定する必要がありました。1つはメインアクティビティ(これ)用で、もう1つはこの問題に関係のないアクティビティ用です。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>

そして、これがPreferenceストレージ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の戻るボタンを押すと、設定ファイルが上記からV-?に変更さH-?れます。ただし、メインアクティビティのSharedPreferencesから値を読み取ると、V-?アプリが再起動するまで、が表示されます。ああ、また、私はOnPreferenceChangeListnener()PreferenceActivityにあり、値が変更されたときに呼び出されます。

最終編集:どうやら、それは与えられた活動のための名前付きandroid:processの使用です。これは、Google Maps APIが、同じアプリ内の2つの別々のMapActivityが異なる設定を使用できるようにするために必要です。PreferenceActivityが同じnamed-processに移動された場合、上記のコードは、の設定を読み取るとonResume()正しい値を返します。

4

2 に答える 2

0

app_preferences.xmlすべてがチェックアウトされているので、キーの PreferenceActivityファイルにタイプミスがあるか、正しくないか未定義のキーがあると思いますR.string.option_showSatellite(その文字列が何であれ)。これにより、同じ値を指していると思われる [あなたには知られていない] 異なる名前を持つ 2 つのキーが生成されます。実際には、prefs アクティビティが 1 つのキーを使用し、Preference クラスがもう 1 つのキーを使用しているため、2 つの異なるキーと 2 つの異なる値が生成されます。

キーを再確認してください。リテラル"R.string.options_showSatellite"を xml ファイルのキーとして使用しているのではなく、実際の文字列を使用していることを確認してください。ローカライズされたバージョンを使用する場合は@string/options_showSatellite、キーで機能します。ただし、キーをローカライズする必要はありません。

興味がある場合は、標準のテキスト エディターでアプリのデータ ディレクトリに設定マネージャーによって作成された設定ファイルを開くことで、これを再確認できます。

于 2012-04-14T03:11:10.643 に答える
-1

onResume SharedPreferences への新しい参照を取得する必要があります。それ以外の場合は、既にメモリ内にある (そして古い値を持つ) オブジェクトを使用しているだけです。

編集:理解できない回答に反対票を投じるのではなく、代わりに説明を求めてみませんか?私が言いたいのは、Pref を正しい方法でプルする必要があるということです (実際の方法ではなく)...

 SharedPreferences sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);

そして、何が起こるか見てください。

于 2012-04-13T21:04:54.537 に答える