我正在尝试制作一个仅在应用首次运行后显示的弹出对话框,它会提醒用户应用中的新更改.所以我有一个这样的对话框弹出:
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:首次运行弹出对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!