Spinner's onItemSelected callback called twice after a rotation if non-zero position is selected

后端 未结 8 1037
执念已碎
执念已碎 2021-02-03 20:52

When I create my activity, I setup a Spinner, assigning it a listener and an initial value. I know that the onItemSelected callback is called automatically during a

8条回答
  •  鱼传尺愫
    2021-02-03 21:30

    Just use setSelection(#, false) before setting the listener:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        spinner.setSelection(2, false);
        spinner.setOnItemSelectedListener(this);
    }
    

    The key is the second parameter. It says to not animate the transition, executing the action immediately and preventing onItemSelected from being fired twice, because a call is already made by the system.

提交回复
热议问题