0

我目前正在编写一个蓝牙应用程序,只允许用户在肖像模式下使用它。但是,还有一些其他原因导致配置更改迫使我向用户显示蓝牙连接已丢失。

在连接到设备时,我会显示一个提示密码的对话框。如果在此对话框可见时发生配置更改,我想将其关闭。因此,我调用了一个辅助方法onSaveInstanceStateonDestroy以及onCreate(如果保存的实例状态不为空)。

@Override
public void onCreate(Bundle savedInstanceState)
{
    if (savedInstanceState != null)
    {
        this.tryDismiss();
    }
    super.onCreate(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState)
{
    System.out.println("Saving instance state!");
    this.tryDismiss();

    super.onSaveInstanceState(outState);
}

@Override
public void onDestroy()
{
    System.out.println("Destroying dialogue!");
    this.tryDismiss();

    super.onDestroy();
}

public void tryDismiss()
{
    try
    {
        System.out.println("DIE BART, DIE!");
        this.dismissAllowingStateLoss();
    }
    catch (Exception closingException)
    {
    }
}

然而,对话仍然显示给用户。我已经阅读了类似的问题,但无法找到解决此问题的方法。

我很感激任何帮助。提前致谢!

4

2 回答 2

0

您可以轻松地dismiss()方法

http://developer.android.com/reference/android/app/Dialog.html

于 2015-07-10T15:21:08.930 回答
0

在显示对话的活动中使用强引用并调用dismiss()活动方法中的onSaveInstanceState方法为我解决了这个问题。我知道,这不是最佳解决方案,但它确实有效。

@Override
protected void onSaveInstanceState(Bundle persistantState)
{
    if (this.connectionPasswordDialogue != null)
    {
        this.connectionPasswordDialogue.dismiss();
    }
}

强引用是指活动内部使用的私有属性:

private DialogueUserModePassword connectionPasswordDialogue;
于 2015-07-13T17:55:42.640 回答