Android:首次运行弹出对话框

Android: First run popup dialog(Android:首次运行弹出对话框)
本文介绍了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.

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

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:首次运行弹出对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)