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


基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01