Search code examples
androidinstance

How to avoid multiple instance of app in recent cards?


I have some troubles with amount of instance my app. In some cases my application has more that one instance in list of recent cards.

Case 1

Noramlly open app. Action main is SplashScreen.class > MainActivity.class > Fragment(view). In this case application has only one instance in recent cards.

Case 2

Application is open, then can open app through mail box, specially from gmail, email include attachment which can open my app. Flow to this case SharedData.class > MainActivity.class > Fragment(view). Now I have two instance of app.

enter image description here

This what I would have is close previous instance and start new or open attachment in first instance.

Tested some parameters for activity from documentation App manifest file - activity. Result is not satisfied, specially tested lunchmode ("singleTop","singleTask" and "singleInstance"). Close one is "singleTask" which close previous instance, but must click twice attachment to open new instance (Instance exist > click attachment > First instance is close (not exist any instance) > click attachment > open new instance).

If someone has advice how resolve this problem I will be appreciate. This problem exist in many combination on stack but none deal with it.


Solution

  • I search little later and found solution:

    set android:launchMode="singleTask" in AndroidManifest on

    <activity
        android:launchMode="singleTask"
        android:name=".MainActivity">
    </activity>
    

    then in SharedData.class set

    private void startMainActivityAndFinishCurrent() {
        Intent intent = new Intent(ContextInstance.getContext(), MainActivity.class);
        startActivity(intent);
        finish();
    }
    

    Thats it, but what's most interesting thing is understand activity tag in manifest