Android:首先运行弹出对话框 [英] Android: First run popup dialog

查看:138
本文介绍了Android:首先运行弹出对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弹出一个弹出式对话框,仅在应用程序第一次运行后才会显示,以提醒用户应用程序的新更改。所以我有一个这样的对话框弹出窗口:

I am trying to make a popup dialog that only shows after the app's first run that will alert users of the new changes in the app. So I have a dialog popup like this:

new AlertDialog.Builder(this).setTitle("First Run").setMessage("This only pops up once").setNeutralButton("OK", null).show();

一旦他们关闭它,它将不会回到下一次更新或重新安装该应用程序。

Once they dismiss it, it won't come back until the next update or they reinstall the app.

如何将上述对话框代码设置为仅运行一次?

How can I set the dialog code above to run only once?

推荐答案

使用 SharedPreferences 存储 isFirstRun 值,并根据该值检查您的启动活动。

Use SharedPreferences to store the isFirstRun value, and check in your launching activity against that value.

如果设置了值,则不需要显示对话框。否则,显示对话框并在SharedPreferences中保存 isFirstRun 标志。

If the value is set, then no need to display the dialog. If else, display the dialog and save the isFirstRun flag in SharedPreferences.

示例:

public void checkFirstRun() {
    boolean isFirstRun = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getBoolean("isFirstRun", true);
    if (isFirstRun){
        // Place your dialog code here to display the dialog

        getSharedPreferences("PREFERENCE", MODE_PRIVATE)
          .edit()
          .putBoolean("isFirstRun", false)
          .apply();
    }
}

这篇关于Android:首先运行弹出对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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