Android导航组件使用深链接:onNewIntent被多次调用。
创始人
2024-10-07 02:00:18
0

当使用深链接启动应用程序时,onNewIntent方法可能会被多次调用。这是因为当应用程序处于后台运行时,系统会使用FLAG_ACTIVITY_NEW_TASK标志启动应用程序并调用onNewIntent方法。

要解决这个问题,可以在onNewIntent方法中添加一个标志,以便只在第一次调用时执行所需的操作。可以使用SharedPreferences来存储这个标志。

以下是一个示例代码,演示如何在Android导航组件中使用深链接,并解决onNewIntent被多次调用的问题:

public class MainActivity extends AppCompatActivity {

    private static final String FIRST_TIME_KEY = "first_time_key";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取Intent中的数据
        Intent intent = getIntent();
        String action = intent.getAction();
        Uri data = intent.getData();

        // 处理深链接数据
        if (action != null && action.equals(Intent.ACTION_VIEW) && data != null) {
            // 检查是否是第一次调用onNewIntent方法
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            boolean isFirstTime = sharedPreferences.getBoolean(FIRST_TIME_KEY, true);

            if (isFirstTime) {
                // 执行所需的操作
                // ...

                // 将标志设置为false,表示已经处理过深链接数据
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean(FIRST_TIME_KEY, false);
                editor.apply();
            }
        }
    }

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

        setIntent(intent); // 更新Intent对象

        // 处理深链接数据
        String action = intent.getAction();
        Uri data = intent.getData();

        if (action != null && action.equals(Intent.ACTION_VIEW) && data != null) {
            // 检查是否是第一次调用onNewIntent方法
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
            boolean isFirstTime = sharedPreferences.getBoolean(FIRST_TIME_KEY, true);

            if (isFirstTime) {
                // 执行所需的操作
                // ...

                // 将标志设置为false,表示已经处理过深链接数据
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putBoolean(FIRST_TIME_KEY, false);
                editor.apply();
            }
        }
    }
}

在这个示例中,我们使用SharedPreferences来存储一个标志,表示是否是第一次调用onNewIntent方法。在onCreate和onNewIntent方法中,我们都会检查这个标志。如果是第一次调用,则执行所需的操作,并将标志设置为false。这样,在后续的onNewIntent调用中,就不会执行重复的操作了。

相关内容

热门资讯

透视好牌"wepok... 1、透视好牌"wepoker新号好一点吗"详细辅助我来教教你(手机版透视脚本技巧)。2、wepoke...
透视游戏"红龙pok... 透视游戏"红龙poker辅助工具"详细辅助教你教程(软件辅助程序软件)1、玩家可以在红龙poker辅...
透视私人局"约局吧透... 1、透视私人局"约局吧透视挂下载"详细辅助攻略教程(轻量版有透视技巧)。2、约局吧透视挂下载透视辅助...
透视挂透视"约局吧a... 透视挂透视"约局吧app有挂吗"详细辅助科技教程(辅助器如何使用技巧)准备好在约局吧app有挂吗 i...
透视线上"poker... 透视线上"poker红龙辅助"详细辅助2025新版(可以提高运气技巧)1、点击下载安装,微扑克wpk...
透视游戏"wpk辅助... 透视游戏"wpk辅助"详细辅助可靠教程(辅助软件视频透明);支持多人共享记分板与复盘,通过邀请好友或...
透视攻略"wepok... 您好,wepoker破解版内购这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
透视免费"wepok... 1、透视免费"wepoker透视脚本免费使用视频"详细辅助德州教程(可以看底牌技巧);详细教程。2、...
透视数据"hhpok... 透视数据"hhpoker是真的还是假的"详细辅助2025新版教程(脚本神器)1、点击下载安装,微扑克...
透视透视挂"wepo... 此外,数据分析德州(wepoker轻量版透视系统)辅助神器app还具备辅助透视行为开挂功能,通过对客...