3

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;
        }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Fernando Santos
  • 392
  • 4
  • 19

2 Answers2

5

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?

johnny_crq
  • 4,291
  • 5
  • 39
  • 64
2

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.

grandpa_sam
  • 119
  • 6