Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9128831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:36:27+00:00 2026-06-17T07:36:27+00:00

I am implementing my own custom DialogPreference subclass that has a SeekBar used for

  • 0

I am implementing my own custom DialogPreference subclass that has a SeekBar used for persisting an integer. I’m a little confused about what needs to go into onSaveInstanceState() and onRestoreInstanceState(). Specifically, do you need to update the UI widget that the user interacts with (in my case, the SeekBar widget) in onRestoreInstanceState()?

The reason I am confused is that the API doc article here tells you to do this:

@Override
protected Parcelable onSaveInstanceState() {
    final Parcelable superState = super.onSaveInstanceState();
    if (isPersistent()) {
        return superState;
    }

    final SavedState myState = new SavedState(superState);
    myState.value = mNewValue; //<------------ saves mNewValue
    return myState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if (state == null || !state.getClass().equals(SavedState.class)) {
        super.onRestoreInstanceState(state);
        return;
    }

    SavedState myState = (SavedState) state;
    super.onRestoreInstanceState(myState.getSuperState());
    mNumberPicker.setValue(myState.value); //<------------ updates the UI widget, not mNewValue!
}

But looking at the source for some official Android Preference classes (EditTextPreference and ListPreference), the UI widget is not updated in onRestoreInstanceState(). Only the underlying value of the Preference is (in the example above, that would be mNewValue).

Here is the relevant source for EditTextPreference:

@Override
protected Parcelable onSaveInstanceState() {
    final Parcelable superState = super.onSaveInstanceState();
    if (isPersistent()) {
        return superState;
    }

    final SavedState myState = new SavedState(superState);
    myState.value = getValue(); //<---- saves mValue
    return myState;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if (state == null || !state.getClass().equals(SavedState.class)) {
        super.onRestoreInstanceState(state);
        return;
    }

    SavedState myState = (SavedState) state;
    super.onRestoreInstanceState(myState.getSuperState());
    setValue(myState.value); //<---- updates mValue, NOT the UI widget!
}

So, what’s the consensus? Where I am supposed to update the UI widget (if I am supposed to update it at all…)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T07:36:28+00:00Added an answer on June 17, 2026 at 7:36 am

    Okay, after some experimentation, it looks like updating the UI widget inside onRestoreInstanceState() is not the way to go, because it always seems to be null at that point. I don’t know why they suggest it. Perhaps you have to do it if subclassing Preference, but there are different rules to follow when subclassing DialogPreference…? That would at least explain why ListPreference and EditTextPreference don’t do it, because they subclass DialogPreference.

    In fact, from what I’ve found, the UI widget does not need to be updated at all! It should have its own save/restore methods that handle its state management for you. For example, here is an excerpt of a DialogPreference subclass I made with a SeekBar widget in it:

    @Override
    protected Parcelable onSaveInstanceState() {
        final Parcelable superState = super.onSaveInstanceState();
    
        final SavedState myState = new SavedState(superState);
        myState.maxValue = getMaxValue(); //<---- saves mMaxValue
        myState.value = getValue(); //<---- saves mValue
        return myState;
    }
    
    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state == null || !state.getClass().equals(SavedState.class))
        {
            super.onRestoreInstanceState(state);
            return;
        }
    
        SavedState myState = (SavedState) state;
        setMaxValue(myState.maxValue); //<---- updates mMaxValue
        setValue(myState.value); //<---- updates mValue
        super.onRestoreInstanceState(myState.getSuperState());
    }
    

    As you can see, I never update a SeekBar widget anywhere. The SeekBar will save/restore its state all by itself!

    You’ll also notice there are some slight deviations from what is suggested in the Android developer docs. I don’t check if the DialogPreference is persistent before saving state, because then the mValue and mMaxValue properties would not get saved if it is. I also call super.onRestoreInstanceState() right at the end, as I found that it never works when it is called earlier.

    These are just my findings so far. I’m not sure what the right way is, but what I have above seems to work.

    UPDATE: @whatyouhide wants to know what the setValue and setMaxValue methods look like in my DialogPreference subclass. Here they are:

    public void setValue(int value)
    {
        value = Math.max(Math.min(value, mMaxValue), mMinValue);
    
        if (value != mValue)
        {
            mValue = value;
            persistInt(value);
            notifyChanged();
        }
    }
    
    public void setMaxValue(int maxValue)
    {
        mMaxValue = maxValue;
        setValue(Math.min(mValue, mMaxValue));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am implementing my own custom DialogPreference subclass, like so: public class MyCustomPreference extends
Where can I get info about implementing my own methods that have the ellipsis
I'm implementing my own double-buffering for a BufferedImage in a JPanel so that I
We have an application that is essentially implementing its own messaging queue. When a
I'm implementing my own <declare-styleable> for a custom View (following the instructions here ).
I'm implementing a Custom RoleProvider in the .NET Membership-framework. The existing functionally needs a
While implementing my own IFilter I found that most likely some consumers will require
I've had a crack at implementing bindings for my own NSView subclass. It works,
I am in the process of implementing scrolling in a custom edit control that
I am working on implementing a custom membership provider that works against an existing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.