[英]Showing the setup screen only on first launch in android
I am making an android application but i can't figure out how i can make the setup screen show up only the first time. 我正在制作一个Android应用程序,但我无法弄清楚如何让设置屏幕只显示第一次。 This is how the application is going to work: User launches the application after installation and is being shown the welcome/setup screen.
这就是应用程序的工作方式:用户在安装后启动应用程序,并显示欢迎/设置屏幕。 And once the user is done with the setup, the setup screens will never appear again unless the user reinstalls the application.
一旦用户完成设置,除非用户重新安装应用程序,否则设置屏幕将永远不会再出现。
How can i make this happen??? 我怎么能让这件事发生? Please help and thanks SO much in advance!
请提前帮助和谢谢!
Use SharedPreferences
to test whether its the first start or not. 使用
SharedPreferences
测试它是否是第一次启动。
Note: The below code was not tested. 注意:以下代码未经过测试。
In your onCreate (or whereever you want to do things depending on first start or not), add 在你的onCreate中(或者你想要根据首次启动而做什么),添加
// here goes standard code
SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);
if(pref.getBoolean("firststart", true)){
// update sharedpreference - another start wont be the first
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("firststart", false);
editor.commit(); // apply changes
// first start, show your dialog | first-run code goes here
}
// here goes standard code
Make one helper activity. 做一个帮助活动。 This will be your launcher activity.It will not contain any layouts, It will just check for first fresh run of an app.
这将是你的启动器活动。它不会包含任何布局,它只会检查应用程序的第一次新运行。 If It will first run, then setup activity will be started otherwise MainActivity will be start.
如果它将首先运行,则将启动安装活动,否则将启动MainActivity。
public class HelperActivity extends Activity {
SharedPreferences prefs = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Perhaps set content view here
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
}
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
//strat DataActivity beacuase its your app first run
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firstrun", false).commit();
startActivity(new Intent(HelperActivity.ths , SetupActivity.class));
finish();
}
else {
startActivity(new Intent(HelperActivity.ths , MainActivity.class));
finish();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.