0

アクティビティを開始する次のコードがあります。このコードは、ユーザーが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;
    }
4

2 に答える 2

1

2つのオプションがあります。

1-アクティビティの起動モードをに変更して、android:launchMode="singleTask"呼び出すたびに新しいインスタンスが作成されないようにしますstartActivity

Bluetooth2-接続を維持しますservice

別の可能な解決策がありますが、洗練されたものではないかもしれませんが、この場合はに緊密になる接続をapplication維持できるカスタムクラスを定義することです。BluetoothapplicationContext

于 2013-02-17T20:55:12.583 に答える
0

別の方法は、同じアクティビティを保持しながら、現在のフラグメントを置き換える新しいフラグメントをアクティビティにロードすることです。FragmentManagerを使用する場合、新しいフラグメントをスタックにプッシュして前方にナビゲートし、それらをポップして後方にナビゲートすることができます。これにより、コンテンツをナビゲートし、Bluetooth接続を維持できます。

于 2013-02-17T20:59:07.680 に答える