我的应用程序中有操作栏。我正在使用 menu.xml 添加操作项。我使用 action bar-compat 作为我的支持库。我观察到一个奇怪的问题,即我的操作项被重复。
当我的设备闲置或使用其他应用程序时,我随机发现了这个问题。请在下面找到屏幕截图和我的代码:
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:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/menu_item_signup"
allergy:showAsAction="ifRoom"
android:title="@string/sign_up">
</item>