我想在应用程序生命周期中只显示一次启动画面。这是我的代码:
SplashScreenActivity.java:
final int welcomeScreenDisplay = 3000;
Thread welcomeThread = new Thread() {
int wait = 0;
@Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(1000);
wait += 1000;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
// Start other Activity
startActivity(new Intent(SplashScreenActivity.this,
MainActiviey.class));
finish();
}
}
};
welcomeThread.start();
清单:
<activity android:name=".SplashScreenActivity" android:label="test"
android:noHistory="true"
android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden|keyboard">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActiviey" android:label="test"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden|keyboard">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
问题是如果我按下硬件HOME按钮隐藏应用程序并再次在应用程序列表中打开应用程序。它将再次显示启动画面(而不是显示MainActivity
)。
是否可以仅在应用程序启动“新鲜”时显示启动画面(不显示在onresume())?谢谢!
答案 0 :(得分:6)
你不能有这两个活动的意图:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
此外,您可能希望隐藏历史堆栈中的启动画面,以防您根据Ash建议决定在其中启动另一个活动。
您可以在活动代码上使用此标记:
android:noHistory="true"
答案 1 :(得分:2)
是的,这是可能的。使用SharedPreferences存储一个标志,表示您的启动已经显示。在启动画面的onCreate()
方法中查看它是否存在,启动下一个活动。
答案 2 :(得分:0)
public class SplashActivity extends AppCompatActivity {
Handler handler;
private final int SPLASH_DISPLAY_LENGTH = 2000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SplashStart();
}
private void SplashStart() {
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
@Override
protected void onResume() {
super.onResume();
}
}