Android 新的活动开始于意图

Android 新的活动开始于意图,android,android-intent,android-activity,Android,Android Intent,Android Activity,我有以下启动活动的代码,该代码用于当用户单击Android设备中的菜单按钮时出现的子菜单,问题是当单击按钮时,一个新的活动开始,从而松开先前形成的蓝牙连接 @Override public boolean onOptionsItemSelected(MenuItem item) { Intent serverIntent = null; Intent PassIntent; Intent PassIntent1; switc

我有以下启动活动的代码,该代码用于当用户单击Android设备中的菜单按钮时出现的子菜单,问题是当单击按钮时,一个新的活动开始,从而松开先前形成的蓝牙连接

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent serverIntent = null;
        Intent PassIntent;
        Intent PassIntent1;
        switch (item.getItemId()) {
        /*case R.id.home:
            // Launch the DeviceListActivity to see devices and do scan
            serverIntent = new Intent(this, engineStarter.class);
            startActivity(serverIntent);
            return true;*/
        /*case R.id.insecure_connect_scan:
            // Launch the DeviceListActivity to see devices and do scan
            serverIntent = new Intent(this, DeviceListActivity.class);
            startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
            return true;*/
        /*case R.id.discoverable:
            // Ensure this device is discoverable by others
            ensureDiscoverable();
            return true;*/
        case R.id.setpassword:
            PassIntent = new Intent(this, SetPassword.class);
            startActivity(PassIntent);
            return true;
        case R.id.home:
            // Launch the DeviceListActivity to see devices and do scan
            serverIntent = new Intent(this, engineStarter.class);
            startActivity(serverIntent);
            return true;
        }
        return false;
    }
您有两个选择:

1-将活动的启动模式更改为
android:launchMode=“singleTask”
,这样它就不会在每次调用
startActivity

2-在
服务中维护
蓝牙连接


还有另一种可能的解决方案,但可能不是优雅的解决方案,是定义一个自定义的
应用程序
类,您可以在其中维护
蓝牙
连接,在这种情况下,该连接将紧密连接到
应用程序上下文
另一种方法是保留相同的活动,但将新的内容加载到活动中,替换当前片段。使用FragmentManager时,您可以将新片段推到堆栈上以向前导航,并将其弹出以向后导航。这将允许您浏览内容并保持蓝牙连接。

我真正想要的是只加载活动的先前状态,而不是重新创建活动并断开连接!为什么不通过服务之类的方式来管理蓝牙连接呢?我试过了,但我记得我在某个地方被卡住了,所以没有那样做!谢谢,我将使用singleTask试用选项1“还有另一个可能的解决方案,但可能不是很好的解决方案,就是定义一个自定义的应用程序类,您可以在其中维护蓝牙连接”-我现在正在做同样的事,但我想我没有将它绑定到相同的上下文,你能解释一下吗?把你的代码发出来,因为我不知道你到底在做什么。