如何仅在应用程序“新鲜”启动时显示启动屏幕?
我想在应用程序生命周期中仅显示一次启动屏幕。这是我的代码:
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>
问题是如果我按硬件主页按钮隐藏应用程序并在应用程序列表中再次打开应用程序。它将再次显示启动屏幕(而不是显示 MainActivity
)。 是否可以仅在应用程序“新鲜”启动时显示启动屏幕(不在 onresume() 处显示)?谢谢!
I want to show the splash screen only once during the Application life cycle. Here is my code:
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();
Manifest:
<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>
The problem is if I press the hardware HOME botton to hide the app and open the App again at the application list. It will show the splash screen again (instead of showing the MainActivity
).
Is it possible to show splash screen only when the app starts "fresh" (not show at onresume() )? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能对两个活动有此意图:
此外,您可能希望从历史记录堆栈中隐藏启动屏幕,以防您决定按照 Ash 建议在其中启动另一个活动。
您可以在活动标签上使用此标志:
You can't have this intent for two activities:
Also you might want to hide the splash screen from the history stack in case you decided launch another activity inside it as per Ash suggestion.
You can use this flag on your activity tag:
是的,这是可能的。使用 SharedPreferences 来存储一个标志,这表明您的启动画面已经已显示。在启动屏幕的
onCreate()
方法中检查它,如果存在,则启动下一个活动。Yes, it's possible. Use SharedPreferences to store a flag, that would indicate that your splash has already been shown. Check it in
onCreate()
method of your splash screen and if it is present, launch the next activity.