JakeWharton/ActionBarSherlock — Issue #93: Spinner's onNavigationItemSelected called too often
Author: erickok
Created: 2011-10-26 15:25:14+00:00
State: open

I use the ActionBar's list navigation mode and react on selection of these items (to show another server/feed/etc.). However, the callback method 'onNavigationItemSelected' is called when it, imo, shouldn't.

A minimal Minimal reproduce example is here: https://bitbucket.org/erickok/absspinnertest/src

The problem is that onNavigationItemSelected is hit when the screen orientation is changed and therefore the activity (or fragment) is recreated. With the native action bar (Android 3.+ and 4.0) the Spinner state is kept but, since no actual new selection is made, the selection changed listener isn't called. With the ABS's pre-3.0 implementation it is.

The specific call on my selection changed listener that I believe should be there - and isn't happening with the native Action bar - is coming from the measurement of the spinner, it seems:

10-26 14:41:16.731: W/System.err(18392): java.lang.Throwable: stack dump
10-26 14:41:16.741: W/System.err(18392):	at java.lang.Thread.dumpStack(Thread.java:577)
10-26 14:41:16.741: W/System.err(18392):	at nl.nl2312.test.absspinner.AbsSpinnerTestActivity$1.onNavigationItemSelected(AbsSpinnerTestActivity.java:39)
10-26 14:41:16.751: W/System.err(18392):	at com.actionbarsherlock.internal.widget.ActionBarView$1.onItemSelected(ActionBarView.java:111)
10-26 14:41:16.751: W/System.err(18392):	at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
10-26 14:41:16.751: W/System.err(18392):	at android.widget.AdapterView.selectionChanged(AdapterView.java:854)
10-26 14:41:16.751: W/System.err(18392):	at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:977)
10-26 14:41:16.761: W/System.err(18392):	at android.widget.AdapterView.handleDataChanged(AdapterView.java:959)
10-26 14:41:16.761: W/System.err(18392):	at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:179)
10-26 14:41:16.761: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.771: W/System.err(18392):	at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
10-26 14:41:16.771: W/System.err(18392):	at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
10-26 14:41:16.771: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.771: W/System.err(18392):	at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
10-26 14:41:16.781: W/System.err(18392):	at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
10-26 14:41:16.781: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.781: W/System.err(18392):	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-26 14:41:16.781: W/System.err(18392):	at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
10-26 14:41:16.791: W/System.err(18392):	at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
10-26 14:41:16.791: W/System.err(18392):	at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
10-26 14:41:16.791: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.801: W/System.err(18392):	at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581)
10-26 14:41:16.801: W/System.err(18392):	at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365)
10-26 14:41:16.801: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.801: W/System.err(18392):	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-26 14:41:16.801: W/System.err(18392):	at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-26 14:41:16.801: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.801: W/System.err(18392):	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-26 14:41:16.801: W/System.err(18392):	at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
10-26 14:41:16.801: W/System.err(18392):	at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
10-26 14:41:16.801: W/System.err(18392):	at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
10-26 14:41:16.801: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.811: W/System.err(18392):	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-26 14:41:16.811: W/System.err(18392):	at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-26 14:41:16.811: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.811: W/System.err(18392):	at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-26 14:41:16.811: W/System.err(18392):	at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-26 14:41:16.811: W/System.err(18392):	at android.view.View.measure(View.java:8313)
10-26 14:41:16.811: W/System.err(18392):	at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
10-26 14:41:16.811: W/System.err(18392):	at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
10-26 14:41:16.811: W/System.err(18392):	at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 14:41:16.811: W/System.err(18392):	at android.os.Looper.loop(Looper.java:130)
10-26 14:41:16.811: W/System.err(18392):	at android.app.ActivityThread.main(ActivityThread.java:3683)
10-26 14:41:16.811: W/System.err(18392):	at java.lang.reflect.Method.invokeNative(Native Method)
10-26 14:41:16.821: W/System.err(18392):	at java.lang.reflect.Method.invoke(Method.java:507)
10-26 14:41:16.821: W/System.err(18392):	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-26 14:41:16.821: W/System.err(18392):	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-26 14:41:16.821: W/System.err(18392):	at dalvik.system.NativeStart.main(Native Method)

Sorry if my explanation is unclear: hopefully the simple example code shows the problem.