Deep link is causing multiple instances of the app

2019-07-23 03:47发布

问题:

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 fragments, I am launching my one activity and then redirecting to the different fragments. The issue I am running into is that multiple instances of the app is opening when clicking on the deep link and when preventing multiple instances of the app from opening I lose the data in my intent for deep linking.

I have prevented multiple instances a couple of ways. One was was by adding singleTop to my manifest

android:launchMode="singleTop"

This prevents multiple instances, however, the static data from my original app instance gets lost. Another way I have also tried the below way as well

   // finishes activity if its not the root activity
    if (!FrameworkUtils.checkIfNull(getIntent().getExtras())) {
        if (!isTaskRoot()) {
            finish();
        }
    }

With this code, I maintain the original instance of the app, but the intent data I need from the deep link is gone because the new instance of the app (which I need) gets closed.

How can I resolve this situation without having to create additional Activities to launch and then doing something like

    Intent intent = getIntent();
    String intentUrl = intent.getDataString();
    Intent newIntent = new Intent(this, MainActivity.class);
    newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    newIntent.putExtra("intentUrl",intentUrl);
    newIntent.setAction(Long.toString(System.currentTimeMillis()));
    startActivity(newIntent);
    finish();

Or rather, how can I remove the original instance of the app and keep the new instance of the app after the user clicks on the deep link? Thanks in advance

回答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.
     */

    public class MyActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            processDataFromIntent();
        }

        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);
            setIntent(intent);
            processDataFromIntent();
        }

        private void processDataFromIntent() {
            Intent intent = getIntent();
            if (intent != null) {
                Bundle extras = intent.getExtras();
                if (extras != null) {
                    String message = extras.getString("data_key");
                }
            }
        }

        public static void startMyActivity(Context context, String data) {
            Intent intent = new Intent(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

    <activity
        android: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.