android – 如何防止onNavigationItemSelected在活动启动时触发?

前端之家收集整理的这篇文章主要介绍了android – 如何防止onNavigationItemSelected在活动启动时触发?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在我的活动中的操作栏中使用微调器是onCreateOptionsMenu:我使用 this tutorial来实现这种方法.我的问题是当活动是午餐时,onNavigationItemSelected方法触发,并且交换机/案例上的代码运行,我为位置0设置的活动打开.当活动是午餐时,我该怎么做以防止运行开关/案例?
@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,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);
}

解决方法

您不需要onCreateOptionsMenu(菜单)中的以下代码

….
….

将其删除并将其放入活动的onCreate(Bundle)方法中.

编辑:

声明一个全局布尔变量:

boolean initializing = true;

将以下代码放在onCreate(Bundle)中:

SpinnerAdapter mSpinnerAdapter;
if(Build.VERSION.SDK_INT <= 10)
{
    mSpinnerAdapter = ArrayAdapter.createFromResource(this,android.R.layout.simple_spinner_item);
}
else
{
    mSpinnerAdapter = ArrayAdapter.createFromResource(this,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,ActivityEvents.class);
                    startActivity(eventsIntent);
                    break;
            }
        }

        return true;
    }
};
//actionBar.setListNavigationCallbacks(mSpinnerAdapter,mOnNavigationListener);
getActionBar().setListNavigationCallbacks(mSpinnerAdapter,mOnNavigationListener);
原文链接:https://www.f2er.com/android/314428.html

猜你在找的Android相关文章