3

I am a new Android developer and I've got my first app nearly complete.

One of the last things I have on my list of things to do is Tasker integration. I have the integration working up to a point.

My app is currently setup to work as a plugin and accept a string from tasker and update a preference with the new string. This is working.

The problem is when I relaunch my app, the value of the preference isn't updating. I can't get the preference summary to update until I completely close the app and relaunch it.

So the question is, how do I force my app to reload the preferences from within the app's onResume or is there a better way all together?

This is the code from the receiver that updates the preference value:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String album_name = bundle.getString(PluginBundleManager.BUNDLE_EXTRA_STRING_MESSAGE);
prefs.edit().putString("text_default_album", StringUtil.sanitize(album_name)).commit();
Log.d(TAG, "fr: "+prefs.getString("text_default_album", ""));

This is the in the activity's onResume:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String album_name = prefs.getString("text_default_album", "");
Log.d(TAG, "album_name: "+album_name);

final Preference album = getPreferenceScreen().findPreference("text_default_album");
album.setSummary(album_name);

The manifest declaration for the receiver:

    <receiver
            android:name=".receivers.FireReceiver"
            android:exported="true"
            android:process=":background"
            tools:ignore="ExportedReceiver" >

        <!-- this Intent filter allows the plug-in to discovered by Locale -->
        <intent-filter>
            <action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING" />
        </intent-filter>
    </receiver>

UPDATE: For anyone who may run into this question and be struggling to figure it out, once I removed the android:process=":background" in the AndroidManifest.xml declaration of the receiver. It now works as expected.

Jayrox
  • 4,335
  • 4
  • 40
  • 43
  • To anyone who is searching for how to update one preference when another changes, also see http://stackoverflow.com/questions/7603633/updating-preferences-in-real-time. – ToolmakerSteve Sep 18 '14 at 01:17

1 Answers1

0

Commit saves the new value in the storage but it doesn't update the one in memory. If you want this functionality you should use apply. Read more here and here

So you must replace

prefs.edit().putString("text_default_album", StringUtil.sanitize(album_name)).commit();

with

prefs.edit().putString("text_default_album", StringUtil.sanitize(album_name)).apply();

Hope this helps...

ChD Computers
  • 3,135
  • 3
  • 23
  • 33
  • That didn't force the preference to update either. When i went back to the preference activity it was still showing the old value. Both worked the same when I closed my app and relaunched it. – Jayrox Jul 15 '13 at 00:48
  • @Jayrox This is strange, the only thing I can think of to replace SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); in your activity with SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); I read somewhere that getApplicationContext() returns a different context than the activity. Give it a try I don't know what else may be... – ChD Computers Jul 15 '13 at 01:06
  • that doesn't seem to make a difference either. – Jayrox Jul 15 '13 at 01:19
  • @Jayrox OK It seems that you have a problem similar to [this one](http://stackoverflow.com/questions/10098981/sharedpreferences-in-broadcastreceiver-seems-to-not-update) or to [this](http://stackoverflow.com/questions/10465318/broadcast-receiver-not-affecting-shared-preferences-in-android-4-0-probably-3-1) the only diff between your app and mine (which is working as expected) is that I do not use a broadcast receiver to update sharedpreferences, I do it in an activity... – ChD Computers Jul 15 '13 at 01:31
  • i've added the manifest receiver code to the original question – Jayrox Jul 15 '13 at 01:35
  • 1
    I just removed the `android:process=":background"` code and now it works as I expected. Thanks! – Jayrox Jul 15 '13 at 01:39
  • 1
    @Jayrox There is a drawback, now your receiver runs on the main thread so keep the onReceive as small as possible... – ChD Computers Jul 15 '13 at 01:42
  • I figure that would be fine for my usage. All it does is grab the intent, trim the string, sanitize out illegal path characters and save the preference. A majority of the time my app is in the background. However, others who may run into this question may want to be a little more cautious. – Jayrox Jul 15 '13 at 01:45
  • 4
    -1: *"Commit saves the new value in the storage but it doesn't update the one in memory"* - that can't be true. If it was, then every *get* of a preference would be forced to go to storage. This would completely defeat the purpose of having an in-memory copy. – ToolmakerSteve Sep 18 '14 at 01:04
  • does not work. the only difference between apply and commit is that apply is asynchronous. – njzk2 Jan 19 '15 at 19:52