我如何强制当前线程等待另一个线程完成后再继续。
在我的程序中,用户从 AlertDialog
中选择一个 MODE
,我想在继续之前停止执行程序,因为该模式包含对游戏玩法的重要配置。
new AlertDialog.Builder(this)
.setItems(R.array.game_modes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
setMode(TRAINING_MODE);
case 1:
setMode(QUIZ_MODE);
default:
setMode(TRAINING_MODE);
break;
}
//continue loading the rest of onCreate();
contineOnCreate();
}
})
.create().show();
如果这是不可能的,谁能给出一个可能的解决方案?
最佳答案
How would I force the current thread to wait until another has finished before continuing.
你不知道。
In my program the user selects a MODE from an AlertDialog, I want to halt executing of the program before continuing as the mode holds important configuration for the gameplay.
不,你不知道。您想要退出主应用程序线程(因此会出现对话框),然后在用户通过某种方式关闭对话框后开始游戏。
because the Alert loads in it's own thread therefore allowing the main thread to continue loading as normal
不,它没有。所有 UI 都在主应用程序线程上完成,包括对话框。
No because some of the onStart() code controls activity state when the Activity loses focus and needs to be re-configured when the activity re-gains focus.
然后在用户通过某种方式关闭对话框之前不要执行任何操作。
另一种选择:将您的“对话框”UI 选项移动到它自己的 Activity 中,让用户在前往游戏 Activity 的途中通过它。
关于android - 在继续执行主线程之前等待 DialogActivity 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2888200/