显示对话框时暂停Android活动

时间:2013-05-16 17:59:04

标签: java android android-activity dialog onpause

我对活动和对话互动有疑问。在我的Android应用程序中,我有一个允许用户扫描QRCode的页面,当扫描有效的QRCode时,应用程序将打开一个Dialog。问题是我想在显示对话框时禁用后台活动中的自动扫描。 当对话框在前台时,有没有办法在后台禁用活动?(即使有问题的对话是在同一个活动上?)。

我试图调用“onPause()”方法,但它不起作用..

感谢您的帮助!

如有必要,我会使用部分代码编辑此帖子:)

1 个答案:

答案 0 :(得分:0)

试试这段代码:

public class MyClass extends Activity {

    private boolean mustScan = true;

    protected void onCreate(Bundle b) {
        if (mustScan) {
            // Just scan
            // Note: If you use a Thread with a while loop, use this:
            // while (true) {if (mustScan) {} }
        }
    }

    // This is the method you use to show the Dialog
    private void showResults (SomeResults here) {
        // Show the dialog
        // ...
        mustScan = false;
        // Also, in the OnClickListener of the buttons add "mustScan = true;"
    }
相关问题