0

I have an activity that has onReceive (battery broadcast) and a service, that inserts data to Shared Preferences. Then from activity I get that data. My problem is that data is only refreshed when I change orientation of the phone. If I do not do that, Activity shows old Shared Preferences data. Any help will be appreciated.

What I would like to know:

  • How to update my activity in Broadscast onReceive method.
  • Why is SharedPreferences are not represented correctly?
whiteLT
  • 338
  • 7
  • 21
  • Are you registering the receiver in your activity or your manifest? – Justin Breitfeller Mar 11 '13 at 13:28
  • Yes in activity, and Battery level, temperature updates in my activity, bus shared preferences does not. Maybe I need to make a time delay? And then try to reach Shared Preferences? – whiteLT Mar 11 '13 at 13:29

3 Answers3

1

Two ideas:

1) When you are editing your shared preferences in your service, be sure to call apply() or commit() to actually update your shared preferences.

2) In your activity, make sure you register a shared preferences listener via registerSharedPreferenceListener so you know when your preferences are updated in your activity.

Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
  • 1) checked, and if it would not be called, text would not change on orientation changed. 2) Going to do that now :) – whiteLT Mar 11 '13 at 13:47
  • How to implement shared preferences listener in onReceive method? – whiteLT Mar 11 '13 at 14:53
  • You implement it in your activity. – Justin Breitfeller Mar 11 '13 at 15:08
  • Do you have an example? Because now I have separate class of Shared Preferences and Activity which has `appPrefs = new AppPreferences(getApplicationContext());` – whiteLT Mar 11 '13 at 15:18
  • Add a method to AppPreferences that will call registerSharedPreferenceListener. Make sure you call that from your activity and create a listener to handle the events. http://stackoverflow.com/questions/3799038/onsharedpreferencechanged-not-fired-if-change-occurs-in-separate-activity selected answer shows one example of this. You can decide when it is appropriate to stop / start listening for changes. – Justin Breitfeller Mar 11 '13 at 15:30
1

When you rotate your phone the activity is recreated. You can finish and start your activity again with:

finish();
startActivity(getIntent());

I dont know if this is the best option for your case, but I refresh my activity with webservices using this and it works properly!

1

to do this I just restart the activity manually ... Here is the metho I used

public void reload() {

    Intent intent = getIntent();
    overridePendingTransition(0, 0);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();

    overridePendingTransition(0, 0);
    startActivity(intent);
}

by using this method there will be no animation due to transition..

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Nope, I don't know why but nothing happens – whiteLT Mar 11 '13 at 13:54
  • check your oncreate method.. as I used no animation so there will be no animation so visually there will be no change.. you can remove secon third and 5th line of the method – stinepike Mar 11 '13 at 14:04
  • I need to use this method int onCreate? It it possible to use it in OnReceive? – whiteLT Mar 11 '13 at 14:09
  • I get force closed(it all vibrates and then shuts down), log http://pastebin.com/p9UNVufm – whiteLT Mar 11 '13 at 14:40
  • please post your code. I am using the same method to do same thing you want – stinepike Mar 12 '13 at 02:43
  • fixed it by making some minor changes: just add this: `private Intent starterIntent;` in your class members area above class name, in my example below main class: `public class MainActivity extends Activity { private Intent starterIntent;` Then anywhere you want place a code to refresh activity without showing to user.: `finish(); starterIntent.setFlags(intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(starterIntent); overridePendingTransition(0,0);` – whiteLT Mar 12 '13 at 16:10
  • Just an FYI, this is not a good way to solve your problem. You shouldn't ever restart your activity manually. Reacting to changes you care about and updating the UI is much more efficient than recreating the entire activity every time. – Justin Breitfeller Mar 12 '13 at 17:49