【发布时间】:2018-01-05 13:03:32
【问题描述】:
我有一个带有 3 个带有片段的不同选项卡的选项卡式活动。 我也有一个工具栏,在这个工具栏中我有 1 个静态菜单对象和 1 个动态对象。
我将静态对象(蓝牙连接)放入包含加载到容器中的 Fragment 的 MainActivity 中,并将动态按钮放入需要该按钮的 Fragment 中。
问题是,如果我将菜单的方法声明为 MainActivity,则声明为片段的方法是无用的,当我按下图标时什么也没有发生...... 但是,如果我将管理菜单的方法删除到 MainActivity 中,现在按钮变成片段就可以了......
有什么办法可以解决这个问题吗? 或者唯一的方法是对每个片段重复静态按钮,并为每个片段添加其他片段不需要的特定按钮?
MainActivity 菜单方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_bluetooth:
if (mBluetoothService == null) {
Log.d(TAG, "menu Bluetooth button");
setupComunication();
} else {
mBluetoothService = null;
setupComunication();
}
// Launch the DeviceListActivity to see devices and do scan
Intent serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
//findDevice();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
片段菜单方法:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_dashboard, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_commands:
// User chose the "Settings" item, show the app settings UI...
Intent settingsIntent = new Intent(getActivity(), SettingsActivity.class);
startActivityForResult(settingsIntent, RESULT_SETTINGS);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
【问题讨论】:
标签: java android android-fragments menu