如何在 startActivityForResult() 之前启动 startActivity()
当我执行代码时,仅在 startActivityForResult() 结束后才调用 startActivity() 。如何首先启动startActivity()?我尝试使用线程但没有成功。
// Splash Correct
Intent correct = new Intent("com.quizcontest.alex.SPLASHCORRECT");
startActivity(correct);
Bundle b = new Bundle();
Intent i = new Intent(StartPlaying.this, CorrectAnswer.class);
b.putInt("p1Key", player1Score);
b.putInt("p2Key", player2Score);
b.putInt("rKey", round);
i.putExtras(b);
startActivityForResult(i, 0);
When I execute the code the startActivity() is called only after the startActivityForResult() is over. How can I start the startActivity() first? I tried with threads but I didn't succeeded.
// Splash Correct
Intent correct = new Intent("com.quizcontest.alex.SPLASHCORRECT");
startActivity(correct);
Bundle b = new Bundle();
Intent i = new Intent(StartPlaying.this, CorrectAnswer.class);
b.putInt("p1Key", player1Score);
b.putInt("p2Key", player2Score);
b.putInt("rKey", round);
i.putExtras(b);
startActivityForResult(i, 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
startActivity 不会阻塞。它会导致新线程中发生某些事情,因此它将立即执行其后发生的行。
您似乎正在尝试显示启动画面。请参阅与启动屏幕相关的其他问题:Android SplashScreen 或使用对话框显示启动屏幕的示例:< a href="http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/" rel="nofollow noreferrer">http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/
如果您想要的行为是启动 Activity 1,然后启动 Activity 2,正确的行为是开始活动 1 以获得结果。然后 onActivityResult 将在活动 1 完成时被调用。此时您可以开始活动 2。
startActivity does not block. It causes something to happen in a new thread, so it will immediately execute the lines that happen after it.
It seems like you are trying to show a splash screen. See this other question related to spash screens: Android SplashScreen or this example for displaying a splash screen using a dialog: http://blog.iangclifton.com/2011/01/01/android-splash-screens-done-right/
If the behavior you want is to start activity 1, and then start activity 2, the correct behavior is to start activity 1 for result. Then onActivityResult will be called when activity 1 has completed. At this point you can start activity 2.