简体   繁体   English

启动应用程序时发生的活动

[英]Activity that happens when the app is launched

I'm creating an android app and I want to have an activity happen when the app starts for the FIRST time. 我正在创建一个android应用程序,并且想在该应用程序首次启动时进行一次活动。 When the user closes the app and re-opens the app the same screen will not show again. 当用户关闭应用程序并重新打开应用程序时,相同的屏幕将不再显示。 Kind of like an initial registration. 有点像初始注册。 I have no databases. 我没有数据库。 Please keep the answer simple as I'm only just beginning in android. 请保持答案简单,因为我才刚开始使用android。 So far none of the other similar questions have made no sense. 到目前为止,其他类似问题都没有道理。 Any help will do. 任何帮助都可以。 Thanks, J. 谢谢,J。

You should use SharedPreferences. 您应该使用SharedPreferences。 Have your main activity check a boolean preference (using getSharedPreferences(context, mode)), and launch the other activity (that should only run once) if and only if this preference is either true or false. 让主活动检查一个布尔型偏好设置(使用getSharedPreferences(context,mode)),并在且仅当此偏好设置为true或false时启动另一个活动(该活动只能运行一次)。 (if()/else) Then, in the other activity, have that preference changed using SharedPreferences.editor(). (if()/ else)然后,在另一个活动中,使用SharedPreferences.editor()更改该首选项。 Do not forget to .commit(); 不要忘记.commit(); The next time your main activity starts, it will check this preference again, except it will not launch the other activity, since the preference has been changed. 下次您的主要活动启动时,它将再次检查此首选项,但由于该首选项已更改,因此它将不会启动其他活动。

Hope this helps, good luck. 希望这有帮助,祝你好运。

If you are unable to use a database then I would suggest using the SharedPreferences class to store a boolean variable indicating if your one-time Activity had been displayed. 如果您无法使用数据库,那么我建议您使用SharedPreferences类存储一个布尔变量,布尔变量指示您的一次性Activity是否已显示。 In your initial Activity , you can check this preference. 在初始Activity ,您可以检查此首选项。 If the value is false, you launch your one time Activity and within that one-time Activity , you set the preference to true. 如果该值为false,则启动一次Activity然后在该一次Activity ,将首选项设置为true。 Subsequent launches will never show the one-time Activity as long as the preference value is set properly. 只要正确设置了首选项值,后续的启动将永远不会显示一次性Activity

You may use an empty file as marker to understand if app starts first time. 您可以使用一个空文件作为标记,以了解应用是否首次启动。 During startup if this marker file does not exists it means that app is starting first time and you will perform initial registration and of cource you should create above mentioned empty marker file 在启动过程中,如果该标记文件不存在,则意味着该应用程序是第一次启动,您将执行初始注册,并且您应该创建上述空标记文件

SharedPreferences is the solution, you can record if this is your first time or not. SharedPreferences是解决方案,您可以记录是否是第一次。

SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
int is_first_time = prefs.getInt("IS_FIRST_TIME", 0);

if(0==is_first_time){

  //here you can launch your activity

  //Save that you already launch your FirstTimeActivity
  SharedPreferences prefs = getSharedPreferences(MY_PREFERENCES, Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = prefs.edit();
  editor.putint("IS_FIRST_TIME", 1);
  editor.commit();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM