Onnavigationitemselected In Actionbar Is Being Called At Startup How Can Avoid It?
Solution 1:
As Mark has stated, its not designed to be a menu.
However, here is a quick and dirty approach to ignore the first call:
declare this class field:
//mNaviFirstHit should be initialized to trueprivatebooleanmNaviFirstHit=true;
And in the onNavigationItemSelected:
@OverridepublicbooleanonNavigationItemSelected(int itemPosition, long itemId) {
if (mNaviFirstHit) {
mNaviFirstHit = false;
returntrue;
}
// DO WHAT YOU WOULD NORMALLY DO
}
Solution 2:
i am using ActionBar whit a dropdown menu and onNavigationItemSelected() is called as soon Activity is created
This is not designed to be a "menu", any more than tabs are designed to be a "menu". The list navigation is designed to allow the user to indicate some content for the current activity, typically by replacing a fragment. Action items (e.g., toolbar buttons, action spillover area) are for navigating between activities.
The first item of my dropdown menu is Home the same action as pressing the application icon whit android.R.id.home so when application starts it calls itself.
So, delete that from your "menu". The user can press your app icon on the left to navigate home.
Solution 3:
You can force the correct default option in your "menu" with the following:
bar.setListNavigationCallbacks(mNavigationAdapter, this);
bar.setSelectedNavigationItem(indexOfCurrentActivityInTheMenu);
Counter-intuitively this must be done AFTER setting the callbacks (which to my mind would give the callback a chance to fire with navigation index of 0). For example, my app has two activities ListView and PageView, and I like my navigation menu ordered alphabetically, but the default start-up activity is PageView. So I have the following in PageView's onCreate():
bar.setListNavigationCallbacks(mNavigationAdapter, this);
bar.setSelectedNavigationItem(1);
and in ListView's onCreate():
bar.setListNavigationCallbacks(mNavigationAdapter, this);
bar.setSelectedNavigationItem(0);
Then, no matter how many times I call startActivity(otherView), it will always set the navigation menu correctly.
Solution 4:
First of all thanks to @CommonsWare for reminding us it is an alternative to tabs, so the code should be designed with that perspective. Though it is not as straight forward as it appears, nevertheless we can work around there.
This can be avoided using a flag as well. But, anyways i suggest the approach i took to fix this.
Just make sure it's called once. Besides that make it an inner class than an anonymous. That will prevent it being called every time this part of code is executed. code below:
if(localOnNavigationListener != null)
localOnNavigationListener = newLocalOnNavigationListener();
classLocalOnNavigationListenerimplementsOnNavigationListener{
@OverridepublicbooleanonNavigationItemSelected(int itemPosition, long itemId) {
// do somethingreturntrue;
}
}
And i see a strange event here, if i make it an inner class it doesn't call onNavigationItemSelected(), if we make it an anonymous class, it will fire onNavigationItemSelected method. If anyone can throw some light on this, it will be useful.
Post a Comment for "Onnavigationitemselected In Actionbar Is Being Called At Startup How Can Avoid It?"