How to prevent onNavigationItemSelected fires when starting this operation?

I want to use the spinner in the action bar in my activity below, this is onCreateOptionsMenu: I use this tutorial to achieve this approach. My problem is that in the event of a lunch break, the onNavigationItemSelected method is activated and the code is run at the start of the switch / case and the activity that I set for position 0. What should I do to prevent the switch / case from starting when the activity dines?

@Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.main, menu); SpinnerAdapter mSpinnerAdapter; if(Build.VERSION.SDK_INT <= 10) { mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_item); } else { mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_dropdown_item); } ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener() { @Override public boolean onNavigationItemSelected(int position, long itemId) { switch (position) { case 0: Intent searchIntent = new Intent(ActivitySearchBusiness.this, ActivityFindBusinessCity.class); startActivity(searchIntent); break; case 2: Intent dealsIntent = new Intent(ActivitySearchBusiness.this, ActivityDeals.class); startActivity(dealsIntent); break; case 3: Intent eventsIntent = new Intent(ActivitySearchBusiness.this, ActivityEvents.class); startActivity(eventsIntent); break; } return true; } }; actionBar.setListNavigationCallbacks(mSpinnerAdapter, return super.onCreateOptionsMenu(menu); } 
+7
android actionbarsherlock android-spinner
source share
1 answer

You do not need the following code inside onCreateOptionsMenu(Menu) :

.... .... ....

Delete it and put in your onCreate(Bundle) method.

Edit:

Declare a global boolean variable:

 boolean initializing = true; 

Put the following code inside onCreate(Bundle) :

 SpinnerAdapter mSpinnerAdapter; if(Build.VERSION.SDK_INT <= 10) { mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_item); } else { mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_dropdown_item); } ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener() { @Override public boolean onNavigationItemSelected(int position, long itemId) { if (initializing) { initializing = false; } else { switch (position) { case 0: Intent searchIntent = new Intent(ActivitySearchBusiness.this, ActivityFindBusinessCity.class); startActivity(searchIntent); break; case 2: Intent dealsIntent = new Intent(ActivitySearchBusiness.this, ActivityDeals.class); startActivity(dealsIntent); break; case 3: Intent eventsIntent = new Intent(ActivitySearchBusiness.this, ActivityEvents.class); startActivity(eventsIntent); break; } } return true; } }; //actionBar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener); getActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener); 
+6
source share

All Articles