运行活动只有一次,然后始终运行主要的一个 [英] Run activity only once, then always run the main one
问题描述
正如标题所说,场景是: 在第一次使用的应用程序,显示屏幕A. 一旦你有一个屏幕完成,该按钮会导致你的屏幕B. 从现在起直到永远,屏幕B就永远是主要的屏幕(活动时间),当你启动应用程序。 我想这2天,我不能得到它。 有人请解释一下详细的,甚至更好的把我一个code.rar,所以我可以研究它。我要疯了这个!
As the title says, Scenario is: On first time using the app, Show Screen A. Once you are done with screen A, the button will lead to you Screen B. From now on and forever, the Screen B will always be main "Screen"(Activity?) when you start the app. I am trying this 2 days and i can't get it. Somebody please explain a little detailed, or even better throw me a code.rar so i can research it. I'm going crazy with this!!!
推荐答案
刚刚宣布的活动为您在您的AndroidManifest.xml一个启动活动和活动为您提供的onCreate(内),你可以简单地做到这一点。
Just declare your Activity A as a launcher Activity in your AndroidManifest.xml and inside your Activity A onCreate() you can simply do this
private SharedPreferences mSharedPreferences;
private Editor mEditor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
mSharedPreferences = getSharedPreferences("yourPrefsFileName", Context.MODE_PRIVATE));
mEditor = mSharedPreferences.edit();
if (mSharedPreferences.getBoolean("isfirstTime", true)) {
mEditor.putBoolean("isFirstTime",false);
mEditor.apply();
}else{
startActivity(new Intent(this, ActivityB.class));
overridePendingTransition(0, 0);
finish();
}
}
这篇关于运行活动只有一次,然后始终运行主要的一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!