android - 在继续执行主线程之前等待 DialogActivity 返回

标签 android android-alertdialog

我如何强制当前线程等待另一个线程完成后再继续。

在我的程序中,用户从 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/

相关文章:

java - 无法在Android studio中运行Scratch文件

javascript - 如何使用 google actions sdk 迭代 js 对象数组并在 google Assistant 应用程序中显示列表选择器

android - 找不到 butterknife 的符号类绑定(bind)

java - openweathermap api 获取今天和其余的工作日?

android - 在 AlertDialog 中更改 ListView 的字体大小

android - 尝试第二次打开警报对话框时出现 FatalException

android - 如何从android中的google plus api获取oauth 2.0 token ?

java - 安装时提示用户输入 - Android

Android AlertDialog.Builder 和软键盘

android - AlertDialog 不适用于自定义布局