Search code examples
androidandroid-activityandroid-lifecycle

Can we destroy instance of an app in the background after opening it from a deep link?


Can we destroy instance of an app in the background after opening it from a deep link? Like you need to login first in that app in your phone before you can proceed in the browser. After successful login, is it possible to destroy the activity which is now in the background (in this case the screen is now the browser)?

I was able to finish the activity and in that case it navigates me back to the browser. But when I try to open the RECENT APPS BUTTON it still shows me the application on background.

Thank you for your help.


Solution

  • In AndroidManifest.xml, add android:excludeFromRecents="true" to MainActivity of your app like below:

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

    It should work. For details please check here.

    Note: If above doesn't work then please try by adding android:launchMode="singleInstance" as well.