Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent onNavigationItemSelected fires when the activity is launched?

I want to use spinner in the action bar in my activity below is the onCreateOptionsMenu: I use this tutorial to achieve this approach. My problem is when the activity is lunch, the onNavigationItemSelected method fires and the code on the switch/case run and the activity that I set for the position 0 opens. what should I do to prevent to run the switch/case when the activity is lunch?

@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);
}
like image 253
Misagh Aghakhani Avatar asked Sep 01 '13 19:09

Misagh Aghakhani


1 Answers

You don't need the following code inside onCreateOptionsMenu(Menu):

.... ....

Remove it and place it in your activity's onCreate(Bundle) method.

Edit:

Declare a global boolean variable:

boolean initializing = true;

Place 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);
like image 66
Vikram Avatar answered Oct 06 '22 19:10

Vikram