Android的:首先运行弹出的对话框弹出、对话框、Android

2023-09-12 02:59:14 作者:落霞与孤鹜

我试图让一个弹出对话框,应用程序的第一个,这将提醒在应用程序的新变化,用户运行后显示。所以,我有一个对话框弹出这样的:

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.

如何设置对话框code以上只运行一次?

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

推荐答案

使用Shared$p$pferences存储在isfirstRun 的价值,并为您在针对该值的发射活动。

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

如果该值被设置,则没有必要显示对话框。如果其他人,显示对话框并保存在isfirstRun 标志中共享preferences。

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