Search code examples
androidandroid-activitynavigationback

Issue with Toolbar Back-Button leading back to Parent Activity


I'm working on an app with 3 activities. Main and two others, which can be started from the Main. Both have a Toolbar Back-Button, which leads to the Main, which waits on the back stack. The problem occurs in this scenario:

Starting Activity B from Main. - Leaving the app to Home Screen - Waiting a while, doing some other stuff, ... (or force to close the app in the app-settings) - Relaunching it by using the app-overview (app restarting in Activity B) - pressing the toolbar back-button -> App closes.

At this point I expected it to go back to Main, but it doesn't.

MANIFEST:

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <!-- ACTIVITIES -->
    <activity
        android:name=".MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/app_name"
        android:launchMode="singleInstance"
        android:screenOrientation="portrait">

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

    <activity
        android:name=".ReminderActivity"
        android:label="Reminder"
        android:launchMode="singleTask"
        android:parentActivityName=".MainActivity"
        android:screenOrientation="portrait">

        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="yyyy.yy.yyyy.MainActivity" />
    </activity>

    <activity
        android:name=".PreferencesActivity"
        android:label="Preferences"
        android:launchMode="singleTask"
        android:parentActivityName=".MainActivity"
        android:screenOrientation="portrait">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="yyyy.yy.yyyy.MainActivity" />
    </activity>

    <!-- RECEIVERS -->
    <receiver android:name="yyyy.yy.yyyy.StartupReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <receiver android:name=".NotiDeactivateReceiver"></receiver>

    <!-- ATTENTION: This was auto-generated to add Google Play services to your project for
         App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

ACTIVITY: I used the code I've found here at stackoverflow

public void initToolbar() {

    toolbar = (Toolbar) findViewById(R.id.toolbar2);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == android.R.id.home) {
        finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

I also tried to go "Up" by using NavUtils. Same effect.

I tried some other apps with my scenario and I noticed, that when they get kicked out of memory (or get manually killed by me using the app-settings) they restart at their Main Activity. Mine restarts a the point I left it, but probably without a back stack.

Can you help me figuring out what's wrong here?


Solution

  • Try with replacing attribute of MainActivity in manifest

    android:launchMode="singleInstance"
    

    to

    android:launchMode="singleTop"