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?
4

3 回答 3

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.

于 2013-03-11T13:38:50.850 回答
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!

于 2013-03-11T13:39:20.180 回答
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..

于 2013-03-11T13:40:36.577 回答