Up Navigation 和 singleTop 启动模式

Fer*_*tos 3 android launchmode android-activity up-navigation

我有一个活动 A,当我按下工具栏项时,它使用 startActivity(intent) 启动活动 B。每当我按下后退按钮或向上导航图标时,它都会关闭我的应用程序。我相信这是因为我在我的父活动中使用了 launchMode="singleTop"(我使用它是因为我有一个搜索视图和一个可搜索的配置,因为我不想开始我的另一个活动实例进行搜索)。所以问题是:如何在不关闭我的应用程序的情况下使用向上导航和后退按钮从子活动(B)返回到父活动(A)?我已经搜索过了,我发现了一些关于 onNewIntent() 的信息。如果这是我的解决方案,我应该如何正确使用它?


这是我的清单文件:

        <activity
            android:name="com.example.fernando.inspectionrover.MainActivity"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="android.intent.action.SEARCH" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />
        </activity>
        <activity
            android:name="com.example.fernando.inspectionrover.BluetoothSettingsActivity"
            android:parentActivityName="com.example.fernando.inspectionrover.MainActivity"
            android:screenOrientation="landscape">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.fernando.inspectionrover.MainActivity" />
Run Code Online (Sandbox Code Playgroud)

以下是如何开始我的新活动:

switch (id) {
            case R.id.bluetoothActivity:
                Intent switchActivity = new Intent(this, BluetoothSettingsActivity.class);
                startActivity(switchActivity);
                Log.i(LIFE_CYCLE, "Switching from " + getLocalClassName() + " to Bluetooth Setting Activity");
                finish();
                break;
        }
Run Code Online (Sandbox Code Playgroud)

joh*_*crq 5

Single Top 意味着如果你启动一个已经在顶部的活动,它不会被再次创建,只是恢复了。

返回导航关闭应用程序的原因是因为您finish()在开始新活动后立即调用。这意味着您不再需要该活动,因此将其从堆栈中删除。如果您返回活动 B,该应用程序将关闭,因为没有任何可返回的内容(您finish()还记得吗?