如何像闪屏运行的活动只有一次 [英] How to run an activity only once like Splash screen
问题描述
在我的应用程序,我想只有第一次运行运行闪屏一次,但问题是,我已经放在清单
这行:安卓noHistory =真正的
伟大的工程,如果我preSS后退按钮并退出应用程序,但请注意,该应用程序仍然在后台运行,当我preSS应用程序图标,它可以追溯到再次闪屏,然后我的注册页面。我想直接重定向到注册页面,当我重新打开我的应用程序。
In my app, I would like to run the Splash screen once at first run only but the problem is that I already placed in the Manifest
this line: android:noHistory="true"
which works great if I press back button and exits the app but note that the app is still in the background running, and when I press the app icon it goes back again to the Splash screen then my Registration page. I wanted to be redirected to the Registration page directly when I reopen my application.
我如何做到这一点?提前感谢任何建议。
How do I do this? Thanks ahead for any suggestions.
推荐答案
因此,这里是我做什么,我SplashActivity(的onCreate):
So here's what I did, in my SplashActivity(onCreate):
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", true);
editor.commit();
Intent intent = new Intent(this, RegistrationActivity.class);
startActivity(intent);
SplashActivity(onResume):
SplashActivity(onResume):
@Override
public void onResume() {
super.onResume();
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if (!firstRun) {
Intent intent = new Intent(this, RegistrationActivity.class);
startActivity(intent);
Log.d("TAG1", "firstRun(false): " + Boolean.valueOf(firstRun).toString());
} else {
Log.d("TAG1", "firstRun(true): " + Boolean.valueOf(firstRun).toString());
}
}
在我RegistrationActivity(的onCreate):
In my RegistrationActivity(onCreate):
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();
boolean firstRun = settings.getBoolean("firstRun", true);
Log.d("TAG1", "firstRun: " + Boolean.valueOf(firstRun).toString());
然后禁用后退按钮prevent回去,除非用户presses主页:
And then disabled back button to prevent going back unless the user presses Home:
@Override
public void onBackPressed() {
}
大感谢那些贡献!
Big thanks for those that contributed!
这篇关于如何像闪屏运行的活动只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!