gpt4 book ai didi

android - 如何防止 onNavigationItemSelected 在 Activity 启动时触发?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:34 25 4
gpt4 key购买 nike

我想在下面的 Activity 中的操作栏中使用微调器是 onCreateOptionsMenu:我使用 this tutorial实现这种方法。我的问题是当 Activity 是午餐时,onNavigationItemSelected 方法会触发并且开关/案例上的代码运行并且我为位置 0 设置的 Activity 打开。当 Activity 是午餐时,我应该怎么做才能防止运行 switch/case?

@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);
}

最佳答案

onCreateOptionsMenu(Menu) 中不需要以下代码:

........

将其移除并将其放置在您 Activity 的 onCreate(Bundle) 方法中。

编辑:

声明一个全局boolean变量:

boolean initializing = true;

将以下代码放在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);

关于android - 如何防止 onNavigationItemSelected 在 Activity 启动时触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18562150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com