1

I don't understant why is this happend: I need to create every Activity in single instance, so I put android:launchMode="singleInstance" in Manifest. But when I push 'Home' on the device and open it again the system clears the stack and opens the root Activity, not the last one that was open. How can I keep the last open Activity for the user but keep singleInstance in Manifest?

EDIT: Manifest

...   


    <application
       ...>           

        <activity
            android:name=".ui.login.LoginActivity"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".ui.activity.MainActivity"
            android:label="${app_title_constant}"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <data android:scheme="${scheme}"/>

                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
            </intent-filter>
        </activity>
        <activity
            android:name=".ui.more.service.ScannerActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme"/>
        <activity
            android:name=".ui.activity.CardsListForSendActivity"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"/>

        <activity
            android:name=".ui.activity.CountriesActivity"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"/>
        <activity
            android:name=".ui.activity.StepperCardPurchaseActivity"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait"/>

        <service
            android:name=".service.CacheContactsIntentService"
            android:exported="false"/>
        <service
            android:name=".managers.push.PushNotificationExtenderService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.onesignal.NotificationExtender"/>
            </intent-filter>
        </service>

        <receiver
            android:name=".managers.push.PushBroadcastReceiver"
            android:enabled="true">
        </receiver>
    </application>

</manifest>
alla
  • 529
  • 5
  • 20
  • Is there a reason why you "need to create every Activity in single instance" ? If not, don't do stuff your user doesn't expect. – Bart Friederichs Dec 06 '16 at 11:28
  • @BartFriederichs it's according to the requirements of the client. Plus, I use OneSignal for push notifiations and when you tap on push it opens the MainActivity, then user taps Back and see again MainActivity, so it is a second reason why I need singleInstance – alla Dec 06 '16 at 11:34
  • post your manifest – David Wasser Dec 06 '16 at 11:39
  • Generally speaking, using `launchMode="singleInstance"` is a bad idea. This is only needed if you are building a "launcher" type application. In all other cases, this is usually wrong. – David Wasser Dec 06 '16 at 11:40
  • @DavidWasser I added Manifest. Do you have an opportunity to explain why it's wrong? I thought it's better when the user sees the same screen only once and if there are several MainActivities with different or the same data it's might be confusing. – alla Dec 06 '16 at 11:47
  • 1
    You can manage the activity stack yourself so that you don't have several instances of `MainActivity` if that is what you want. You can use a launch mode like `singleTop` if you want, but you should almost never use `singleTask` or `singleInstance`. – David Wasser Dec 06 '16 at 11:50
  • http://stackoverflow.com/questions/2441203/how-to-make-an-android-app-return-to-the-last-open-activity-when-relaunched – Vinodh Dec 06 '16 at 11:53

1 Answers1

0

Do not use launchMode="singleInstance". This is not necessary, and actually isn't working for you anyway. Normally, specifying "singleInstance" will cause each and every Activity to be launched into its own separate task. This, however, won't happen in your case because you have not specified "taskAffinity" in the manifest. Since all activities, by default, have the same task affinity, they will all end up in the same task (even though you specified "singleInstance" launch mode).

Remove all the launchMode specifiers and try again.

The standard behaviour of Android is to return the user to the task stack (exactly as he left it) when an App is brought from the oforeground to the background (after pressing the HOME button and returning to the app).

David Wasser
  • 93,459
  • 16
  • 209
  • 274