Alexandru Circus
Alexandru Circus

Reputation: 5538

Not returning to parent activity when back pressed

I have 2 activities, A(the root activity) and B which is launched from A. Pressing back from B returns to A except 1 case. If I am in B, go to home screen, launch the app again from recent apps, B comes to foreground but pressing back leads to home screen instead of A. If I get back to the app by launching from icon and not from recent apps, shows me activity A, which, let's say, is correct(although B should be shown and pressing back from B should lead to A).

Acitivity A:

<activity android:name=".AudioRecTabsActivity"
            android:label="@string/app_name"
            android:launchMode="singleInstance">
            <!--  android:configChanges="keyboardHidden|orientation">-->
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

Activity B:

<activity android:name=".settings.SettingsActivityOld"
            android:label="@string/settings_activity_title"/>

Launching act B from A:

intent = new Intent(this, SettingsActivityOld.class);
startActivity(intent);

Upvotes: 1

Views: 1926

Answers (3)

Mohd Mufiz
Mohd Mufiz

Reputation: 2236

Override backpress method of your B activity and start activity A from there

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, A.class);
    startActivity(intent);

}

Upvotes: 0

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

This is perfectly normal if you are using android:launchMode="singleInstance" since Activity B runs in a new Task and Activity A is the only Activity in its Task. Check this link

Upvotes: 3

GreenGodot
GreenGodot

Reputation: 6753

Try calling Activity#finish() in activity B. It should remove the current activity from the activity stack and return you to the previous.

Upvotes: 0

Related Questions