我想在设备首次执行应用程序时显示两个启动活动。 我使用共享偏好来完成这项工作,但这并不是我想要的。
以下是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
super.onCreate(savedInstanceState);
Log.d("test", "on Main 2");
try {
SharedPreferences sharedPreferences = getSharedPreferences("prefs",
0);
SharedPreferences.Editor editor = sharedPreferences.edit();
boolean firstTime = sharedPreferences.getBoolean("first", false);
Log.d("test", "on Main 2 shared Preference");
if (!firstTime) {
Log.d("test", "on Main 2 shared Preference,check if it appeared
for the first time.");
//running for the first time;
editor.putBoolean("first", true);
editor.apply();
//if running for the first time then jump from this activity to
Main3.
Log.d("test", "on Main 2 shared Preference,going to Main3");
Intent intent = new Intent(Main2.this, Main3.class);
startActivity(intent);
Log.d("test", "going to main 3 ");
finish();
} else {
//if not running for the first time then jump from this activity
to Main3.
Intent intent = new Intent(Main2.this, SendMessage.class);
startActivity(intent);
finish();
}
同一段代码用于具有不同类名的其他类。 代码是:
SharedPreferences sharedPreferences = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
boolean firstTime = sharedPreferences.getBoolean("first", false);
if (!firstTime) {
//running for the first time;
editor.putBoolean("first", true);
editor.apply();
//if running for the first time then jump from this activity to Main4.
Intent intent = new Intent(Main3.this, Main4.class);
startActivity(intent);
finish();
} else {
//if not running for the first time then jump from this activity to Main4.
Intent intent = new Intent(Main3.this, Main4.class);
startActivity(intent);
finish();
}
Main4
是我们每次应用启动时要显示的活动。
启动时,应用会直接跳转到Main4
活动而不显示Main2
和Main3
。我认为我的意图存在一些问题。
请提出这个问题的建议!