Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
        }
like image 780
Fernando Santos Avatar asked Sep 06 '25 03:09

Fernando Santos


2 Answers

Single Top means that if you launch an activity that is already on top, it wont be created again just resumed.

The reason your back navigation close the app is because you are calling finish() just after you start a new activity. Which means you don't need that activity anymore so it is removed from the stack. If you go back on activityB, the app will close because there is nothing to go back (you called finish() remember?

like image 142
johnny_crq Avatar answered Sep 08 '25 00:09

johnny_crq


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.

like image 43
grandpa_sam Avatar answered Sep 07 '25 23:09

grandpa_sam