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

  • SEARCH
  • Home
  • 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 8703061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:47:40+00:00 2026-06-13T02:47:40+00:00

In my application, I have a Spinner whose onItemSelected initializes the value of a

  • 0

In my application, I have a Spinner whose onItemSelected initializes the value of a class member, say, a, to its default value of 0. The user may then perform steps to change the value of a. I am saving the value of a in onSaveInstanceState and restoring it in onRestoreInstanceState. However, apparently as a result of restoring of the Spinner, its onItemSelected gets called and puts a back to zero. I want the value entered by the user to be preserved.

Note, however, that the old value of a would not make sense in any other setting of the Spinner (hence zeroing it upon item selection). Unfortunately, it is also hard to validate it. So, not knowing that the call to onItemSelected was fake, putting a to zero is inevitable.

The problem is that the input event handler seems to be put into some queue and called after onCreate / onRestoreInstanceState is finished (twice, which is another thing I don’t understand). My question thus is, at what moment should I restore a to the non-default value from the saved instance state? Should I put this task at the end of the input event queue somehow? Or is there a way of distinguishing whether the call of onItemSelected happened as a result of an actual user input or just instance state restoration?

Code example:

private String KEY = "(...).A";
private int a;

public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id) {
  /* other important stuff */
  a = 0;
  Log.d("info", "a zeroed");
}

public void onSaveInstanceState(Bundle state) {
  super.onSaveInstanceState(state);
  state.putInt(KEY, a);
}

public void onRestoreInstanceState(Bundle state) {
  super.onRestoreInstanceState(state);
  a = state.getInt(KEY, a);
  Log.d("info", "a restored");
}

After a change of orientation, the log says

info  a restored
info  a zeroed
info  a zeroed
  • 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-13T02:47:41+00:00Added an answer on June 13, 2026 at 2:47 am

    Here’s what I came up with so far. It does the trick without counting off two calls of onItemSelected found by a mere observation. However, I still consider it a workaround:

    private String KEY = "(...).A";
    private String SEL = "(...).SEL";
    private int a;
    private int sel = -1;
    
    public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id) {
      /* other important stuff */
      if(position != sel) {
        a = 0;
        Log.d("info", "a zeroed");
        sel = position;
      } else
        Log.d("info", "a kept");
    }
    
    public void onSaveInstanceState(Bundle state) {
      super.onSaveInstanceState(state);
      state.putInt(KEY, a);
      state.putInt(SEL, sel);
    }
    
    public void onRestoreInstanceState(Bundle state) {
      super.onRestoreInstanceState(state);
      a = state.getInt(KEY);
      sel = state.getInt(SEL);
      Log.d("info", "a restored");
    }
    

    After a change of orientation, the log now says

    info  a restored
    info  a kept
    info  a kept
    

    The bad thing about this solution is that a has its value in the time when the Spinner is still uninitialized, which does not cause problems at the time being, but should never happen. A proper solution should let onItemSelected do all of its work and set a after that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in may application, i have a spinner widget that contains huge amount of data.
I've an application where the user keys in his friend's information.My application have Spinner
I have created an application for ContactsContract... I have created a spinner which brings
I have an Android application with a Spinner and want to fill it dynamically
I'm developing an Android application with a spinner. I have this problem: This is
In my console application have an abstract Factory class Listener which contains code for
hi I created one simple xml based spinner application.i got all value in my
I have an application, which has a Spinner that I want populated with some
I would like to have my application populate an empty textview with the value
In my application i have spinners,if there are 10 items in a spinner how

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.