本文介绍了动作条项目被复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的行动吧在我的应用程序。我加入使用menu.xml文件的行动项目。我使用的操作栏,COMPAT我的支持库。我发现一个奇怪的问题,在我的行动项目得到复制。
I have Action Bar in my application. I am adding action items using menu.xml. I am using action bar-compat as my support library. I observed a weird issue where my action items are getting duplicated.
我发现这个问题时,随机离开我的设备闲置,或与其他应用程序。请找到屏幕截图和我下面的code:
I am finding this issue randomly when leave my device idle or work with other applications. Please find the screen shot and my code below:
private LoginWebActivity mContext;
private final String TAG = "LoginFragment";
// for metrics
private String mPageNameSignIn = "signin";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.webview, container, false);
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = (LoginWebActivity) getActivity();
initFragment();
}
@Override
public void onResume() {
super.onResume();
}
/**
* Initialises the views and variables of the fragment.
*/
@SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
protected void initFragment() {
mWebView = (WebView) mView.findViewById(R.id.webView);
Bundle b = mContext.getIntent().getExtras();
if (b != null) {
mUrl = b.getString(Constants.EXTRA_WEB_LOGIN_URL);
}
super.initFragment();
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.signin, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Navigate
switch (item.getItemId()) {
case R.id.menu_item_signup:
mContext.onSignUpClick();
break;
case android.R.id.home:
if (!goBack())
getActivity().finish();
default:
break;
}
return super.onOptionsItemSelected(item);
}
我的XML:
My XML :
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/menu_item_signup"
allergy:showAsAction="ifRoom"
android:title="@string/sign_up">
</item>
推荐答案
您必须加入项目前清除菜单对象。我有同样的问题,这是我已经找到了最好的解决方案。
You must clear your menu object before adding items. I had the same problem and this was the best solution that I've found.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.signin, menu);
super.onCreateOptionsMenu(menu, inflater);
}
这篇关于动作条项目被复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!