共有設定に文字列を書き込もうとしていますが、一見すると動作するようです。アプリの他の部分では、共有の設定にアクセスして文字列を正しく読み取ることができます。共有設定はメモリに保存されますが、ディスクには保存されません
アプリを離れると問題が発生します。共有設定文字列セットのすべてのデータが失われ、空のセットが再び返されます。
アプリが閉じられて再オープンされるまでアクセスできるという事実は、それがメモリに保存されているものの、ディスクには保存されていないと思うようになります。
私はここで多くの答えを読んで、コミットと適用の間で変更しようとしましたが、何が問題の原因かわかりません。
私はそれを保存しようとする方法は、次のとおりです。
- は共有優先して、更新のHashSetを保存
- のHashSetに新しい文字列を追加し、共有好み
- からHashSetのを取得します。ここで
コードです:
public static void storeReminder (Context context, String reminderString){
// Get the set of reminder strings
SharedPreferences sharedPreferences = context.getSharedPreferences("AppData", Context.MODE_PRIVATE);
Set <String> remindersStringSet = sharedPreferences.getStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>());
// Add the new reminder string to the reminder string set
remindersStringSet.add(reminderString);
// Save the reminder string set now that the new reminder string has been added
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putStringSet(context.getResources().getString(R.string.reminders_hashset_key), remindersStringSet);
editor.commit();
}
そして、これは私がアプリの他の部分に蓄積されたHashSetの取得方法です:
// Get the set of reminder strings
SharedPreferences sharedPreferences = context.getSharedPreferences("AppData", Context.MODE_PRIVATE);
Set<String> remindersStringSet = sharedPreferences.getStringSet(context.getResources().getString(R.string.reminders_hashset_key), new HashSet<String>());
あなたの助け
を事前に感謝を
あなたは本当にcontext.getSharedPreferences( "AppData"、Context.MODE_PRIVATE)を使用していますか?両方のget/setで? –
はい、確かです。私は私の質問を編集して、私がそれを取り出す場所を含むようにしました。 –
別の名前付き共有環境設定をアプリケーションで使用する特定の理由がない限り、context.getSharedPreferences( "AppData"、Context.MODE_PRIVATE)ではなく、PreferenceManager.getDefaultSharedPreferences(context)としてデフォルト共有設定に切り替えようとします。 – Kubicko