Skip to content Skip to sidebar Skip to footer

Deep Link Is Causing Multiple Instances Of The App To Open

This issue has been addressed in similar posts, however, my situation is a little different. I have only ONE activity, and multiple fragments. I am not deeplinking into specific fr

Solution 1:

Please find below the activity code which will have only one instance and also you can send your data and process it. Let me know if you have any doubts.

package example.raghavpai;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;

    /**
     * Created by RaghavPai on 09-03-2017.
     */publicclassMyActivityextendsActivity {
        @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            processDataFromIntent();
        }

        @OverrideprotectedvoidonNewIntent(Intent intent) {
            super.onNewIntent(intent);
            setIntent(intent);
            processDataFromIntent();
        }

        privatevoidprocessDataFromIntent() {
            Intentintent= getIntent();
            if (intent != null) {
                Bundleextras= intent.getExtras();
                if (extras != null) {
                    Stringmessage= extras.getString("data_key");
                }
            }
        }

        publicstaticvoidstartMyActivity(Context context, String data) {
            Intentintent=newIntent(context, MyActivity.class);
            intent.putExtra("data_key", data);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

Manifest code for the same

<activityandroid:name=".MyActivity"android:launchMode="singleTask"android:screenOrientation="portrait"android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></activity>

Use the public static API startMyActivity from any of your activities.

Solution 2:

I found a simple solution that is working better for me

just add this

if (!isTaskRoot()) {
        finish();
    }

Post a Comment for "Deep Link Is Causing Multiple Instances Of The App To Open"