深层链接导致该应用的多个实例打开 [英] Deep link is causing multiple instances of the app to open

查看:146
本文介绍了深层链接导致该应用的多个实例打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经在类似的帖子中得到了解决,但是我的情况有所不同.我只有一个活动,并且有多个片段.我没有深入链接到特定的片段,而是启动了一个活动,然后重定向到不同的片段.我遇到的问题是,当单击深层链接时,该应用程序的多个实例正在打开;当阻止打开该应用程序的多个实例时,我失去了深层链接的数据.

我已经通过多种方式阻止了多个实例.一种是在清单中添加singleTop

android:launchMode="singleTop"

这可以防止多个实例,但是,原始应用程序实例中的静态数据会丢失.我也尝试过以下方法的另一种方法

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

使用此代码,我维护了该应用程序的原始实例,但是我从深层链接中获取的意图数据已经消失了,因为该应用程序的新实例(我需要的)已关闭.

如何解决这种情况而不必创建其他活动来启动,然后执行类似的操作

    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();

或者更确切地说,在用户单击深层链接后,如何删除应用程序的原始实例并保留应用程序的新实例?预先感谢

解决方案

请在下面找到仅包含一个实例的活动代码,您还可以发送数据并对其进行处理.如果您有任何疑问,请告诉我.

    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);
        }
    }

相同的清单代码

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

从您的任何活动中使用公共静态API startMyActivity.

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

解决方案

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.

这篇关于深层链接导致该应用的多个实例打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆