android 退出时闪屏很奇怪
我为我的 Android 应用程序创建了一个闪屏。我沿着路线走下去,它自己的活动在启动线程中显示自己,然后加载“MainMenu”活动。 这工作正常,直到我想退出该应用程序。 当我点击“后退按钮”时,我看到了主菜单窗口。当我第二次点击“后退按钮”时..我没有看到启动画面,但我再次看到了主菜单。额外的“返回”将结束应用程序。
不好,有什么关于如何避免这种行为的好的提示吗? 当然,最好的方法是在“主菜单”中点击“返回”时直接结束应用程序,但我想我随后需要重新建模启动屏幕以成为该活动的一部分?
飞溅代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread splashThread = new Thread() {
@Override
public void run() {
try {
Log.d("Trying","Tries");
int waited = 0;
while (waited < 5000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
Log.d("Catching", e.toString());
} finally {
finish();
Intent i = new Intent(UEABB.this,MainMenu.class);
UEABB.this.startActivity(i);
startActivity(i);
}
}
};
splashThread.start();
}
问候
I have created a splashscreen for my android app. I went down the route with an activity of it's own displaying itself in a splashthread and then loading the "MainMenu" acitivty.
This works alright, until I want to quit the app.
When I hit the "back button" I see the MainMenu window. When I hit the "back button" a second time .. I don't see the splashscreen I see the MainMenu once more. An additional "back" will end the app.
Not nice, are there any good hints on how to avoid that behaviour?
Best of all would of course be to end the app directly when hitting "back" form the "MainMenu" but I guess I then need to re-model the splashscreen to be a part of that activity instead?
Splashcode
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread splashThread = new Thread() {
@Override
public void run() {
try {
Log.d("Trying","Tries");
int waited = 0;
while (waited < 5000) {
sleep(100);
waited += 100;
}
} catch (InterruptedException e) {
Log.d("Catching", e.toString());
} finally {
finish();
Intent i = new Intent(UEABB.this,MainMenu.class);
UEABB.this.startActivity(i);
startActivity(i);
}
}
};
splashThread.start();
}
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在您的manifest.xml 中的SplashScreen Activity 上显式设置
android:noHistory="true"
。我在设计“新增内容”活动时遵循了类似的方法。不过,一旦切换到另一个 Activity,您就应该调用
finish()
。Try to explicitly set the
android:noHistory="true"
on the SplashScreen Activity in your manifest.xml. I followed a similar approach when designing my "What's New" Activity.Nevertheless you should call
finish()
once you switch to another activity.您的splashthread 活动应在启动MainMenu 活动后立即调用
finish()
。这会将其从堆栈中删除,并且不会干扰退出应用程序。除非您在事件处理程序中使用后退键,否则我想不出任何其他可能导致此行为的事情。也许您可以发布splashthread 的代码。
Your splashthread activity should call
finish()
immediately after starting the MainMenu activity. That will remove it from the stack and it shouldn't interfere with exiting the app.Unless you are consuming the back key in an event handler, I can't think of anything else off the top of my head that could cause this behavior. Perhaps you could post the code for splashthread.