gpt4 book ai didi

android - 安装后仅启动一次 Activity

转载 作者:行者123 更新时间:2023-11-29 17:08:45 25 4
gpt4 key购买 nike

我有 QR 扫描仪应用程序。应用程序中有 3 个 Activity 。

1) 主要 Activity - 打开相机并开始扫描的按钮

2) 二维码 Activity - 扫描二维码

3) Web Activity - 成功扫描后,在应用程序中打开网页

在这里,Main activity 和 QR activity 应该仅在初始安装后启动一次。我在某处读到有关使用共享首选项的信息。但是我有点困惑我在哪里检查变量,就像在哪个 Activity 中一样。我应该在主 Activity 中检查我的共享变量吗?

这是我的第一个应用程序。对不起,如果这是一个愚蠢的疑问。

最佳答案

这是正确的,你必须用SharedPreferences来做.

Here is a good explaination about how to use them

在显示的第一个 Activity 上,您必须在 onCreate 方法中添加这些行:

//this retrieve the sharedpreference element
SharedPreference myPref = this.getSharedPreferences(
"prefName", Context.MODE_PRIVATE);
//this retrieve the boolean "firstRun", if it doesn't exists, it places "true"
var firstLaunch = myPref.getBoolean("firstLaunch", true);
//so, if it's not the first run do stuffs
if(!firstLaunch){
//start the next activity
finish();
}
//else, if it's the first run, add the sharedPref
myPref.edit().putBoolean("firstLaunch", false).commit();

希望对你有帮助

关于android - 安装后仅启动一次 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41167285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com