Ajay
Ajay

Reputation: 31

Android: Service restart after preferences change

I have a Service and a PreferenceActivity that allows the user to edit some preferences. I would like to restart the Service once the user is done with the PreferenceActivity.

I understand that I can register onChange listeners for individual preference changes, but I do not want to restart the Service as each preference changes. I would like to do this when the user is done editing all the preferences. Short of having a "Apply Now" button in the PreferenceActivity, I do not see a straightforward way of doing this.

Am I missing something fundamental here?

Thanks!

Upvotes: 3

Views: 3252

Answers (2)

larham1
larham1

Reputation: 12236

Another alternative is to override onStart() in your PreferenceActivity to record the "before" values, and override its onStop() to check for any deltas, so that they can be handled all at once, e.g.

@Override
protected void onStart() {
    super.onStart();
    // save current state into data member(s) for comparison later
    mShouldNotify = getApp().getSettings().getBoolean(PrefsActivity.PREF_SHOW_NOTIFICATIONS, true);           
}

@Override
protected void onStop() {
    super.onStop();

    if (mShouldNotify != getApp().getSettings().getBoolean(PrefsActivity.PREF_SHOW_NOTIFICATIONS, true)) {
        // we changed notifcation status.  Tell google.
        getApp().updateGooglePushRegistration();
    } 
}

Upvotes: 1

codelark
codelark

Reputation: 12334

In the Activity that starts the PreferenceActivity use startActivityForResult and onActivityResult to track when the user has finished the PreferenceActivity and restart the service there.

eg.

Wherever you're starting the PreferenceActivity:

Intent prefIntent = new Intent(this, MyPreferenceActivity.class);
startActivityForResult(prefIntent, PREFS_UPDATED);

later in that same Activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (resultCode) {
        case PREFS_UPDATED:
            // restart service
            break;
        ...
    }
}

Upvotes: 3

Related Questions