我可以只在第一次打开应用程序时运行 android 活动吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3010457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I have an android activity run only on the first time an application is opened?
提问by The Trav
OK, so I'm playing around with an android app.
好的,所以我正在玩一个 android 应用程序。
The 90% use case is that users want to go straight to the primary list screen to find what they're looking for. That's what I want as my default screen.
90% 的用例是用户希望直接进入主列表屏幕以找到他们正在寻找的内容。这就是我想要的默认屏幕。
The first time a user loads the app however, some configuration is required before their list screen is of any value to them.
然而,用户第一次加载应用程序时,需要进行一些配置,然后列表屏幕才能对他们产生任何价值。
So my question, is how I can go about displaying the configuration activity the first time the app is opened up, and then the list screen for future openings.
所以我的问题是,如何在第一次打开应用程序时显示配置活动,然后显示未来打开的列表屏幕。
I also want to put a demo button on the configuration screen, so I suppose more than just detecting that it's the first time, I specifically want to detect whether the user has performed certain configurations within the first screen.
我还想在配置屏幕上放一个演示按钮,所以我想不仅仅是检测这是第一次,我特别想检测用户是否在第一个屏幕内执行了某些配置。
采纳答案by mtmurdock
i like dweebsonduty's method. a similar way to do this is to save their configuration information in files on the SD card. your app could start out by checking for those files and loading the information. if the load is successful, it moves on to the next activity, if not it starts the appropriate activity to prompt the user for input.
我喜欢 dweebsonduty 的方法。一种类似的方法是将它们的配置信息保存在 SD 卡上的文件中。您的应用程序可以通过检查这些文件并加载信息来开始。如果加载成功,它会移动到下一个活动,否则它会启动适当的活动来提示用户输入。
I have done this same thing, but instead of swiching activities i just switch views until i have all the info i need, and then move on.
我也做过同样的事情,但我没有切换活动,而是切换视图,直到获得所需的所有信息,然后继续。
回答by Primal Pappachan
After the first time the user has loaded the app you could store the details of whether user has performed the configurations in SharedPreferences.
在用户第一次加载应用程序后,您可以在 SharedPreferences 中存储用户是否已执行配置的详细信息。
protected void storeSharedPrefs(String value) {
/*
* Storing in Shared Preferences
*/
editor.putString("first", value);
editor.commit(); //Commiting changes
}
Check each on time application is loaded, whether its the first time and configuration details has been entered correctly by checking SharedPreferences
通过检查 SharedPreferences 检查每个应用程序是否按时加载,是否第一次和配置详细信息已正确输入
private boolean first_time_check() {
/*
* Checking Shared Preferences if the user had pressed
* the remember me button last time he logged in
* */
String first = uPreferences.getString("first", null);
if((first == null)){
return false;
}
else
return true;
}
回答by Guzba
Many applications actually store the current version in SharedPreferences, and check against it for if an update has been installed. Its a clever way of achieving a "what's changed" popup, or making sure that some settings get set (I would be wary of just having a boolean flag because if you ever want to add an additional setting, you will need a second flag and it gets messy after the third, etc.).
许多应用程序实际上将当前版本存储在 SharedPreferences 中,并检查是否已安装更新。这是实现“更改内容”弹出窗口或确保设置某些设置的巧妙方法(我会担心仅使用布尔标志,因为如果您想添加其他设置,则需要第二个标志和在第三次之后它会变得凌乱,等等)。
回答by Braxton Nunnally
String VersionValue = "v.1.0"; final String PREFS_NAME = "MyPrefsFile";
String VersionValue = "v.1.0"; final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean(VersionValue, true)) {
//the app is being launched for first time, do something
NewDialogFragment newFragment = new NewDialogFragment();
newFragment.show(getFragmentManager(), "New");
// record the fact that the app has been started at least once
settings.edit().putBoolean(VersionValue, false).commit();
}
You could do it this way and still get the same result I tried it its a small workaround if u do not fully understand how to check if the app is updated. Instead with this code you can just simply change the String VersoinValue to your current app version and android will think the app is a new first time app and will only display the code u wrote once until you change VersionValue on your next update. (:
你可以这样做,仍然得到相同的结果,如果你不完全理解如何检查应用程序是否更新,我试过它是一个小的解决方法。相反,使用此代码,您只需将 String VersoinValue 更改为您当前的应用程序版本,android 就会认为该应用程序是新的首次应用程序,并且只会显示您编写的代码,直到您在下次更新时更改 VersionValue。(:
回答by shaneburgess
How will you be storing the configuration?
您将如何存储配置?
If it is in SQLlite you could just create a table called firstuse and put a field in there called hasbeenused and make it null. Then when the app is used you can put a 1 in there. Then you can read it each time your app loads and if that field = 1 then go to your next activity.
如果它在 SQLlite 中,您可以创建一个名为 firstuse 的表,并在其中放置一个名为 hasbeenused 的字段并使其为空。然后在使用该应用程序时,您可以在其中输入 1。然后您可以在每次应用加载时阅读它,如果该字段 = 1,则转到下一个活动。