androidandroid-activitylaunchmodeup-navigation

Up Navigation and singleTop launch mode


I have an activity A, and when I press an toolbar item, it starts activity B using startActivity(intent). Whenever I press back button or the up navigation icon, it closes my app. I believe it's because I'm using launchMode="singleTop" in my parent activity (I'm using this because I have a Search View and a searchable configuration, because I don't want to start another instance of my activity on for search). So the question is: How can I get back from child activity(B) to parent activity(A) using both up navigation and back button without closing my app? I've searched about it, and I found something about onNewIntent(). If this is my solution, how should I use it properly?


Here is my manifest file:

        <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" />

Here is how start my new activity:

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;
        }

Solution

  • I may be just poking at the easiest answer but I think the main problem is that you are calling finish after you start the new activity. This calls on destroy for the calling activity and removes it from the activity stack.