即使Context发生变化,也会显示DialogFragment

时间:2016-12-31 08:08:09

标签: android dialog android-dialogfragment

我有应用程序发送了哪条消息,并在BroadcastReceiver之后的某个时间收到了其报告。此报告将通过我正在使用DialogFragment的对话框显示给用户,如下所示。

myReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(firstTime)
            firstTime = false; 

        boolean anyError = false;
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            case SmsManager.RESULT_ERROR_NO_SERVICE:
            case SmsManager.RESULT_ERROR_NULL_PDU:
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                anyError = true;
                break;
        }

        sent.add(anyError);
        CustomAlertDialogFragment customAlertDialogFragment = CustomAlertDialogFragment.newInstance("Title",logMessage);
        customAlertDialogFragment.show(getActivity().getSupportFragmentManager(),"TAG");
        sent.clear();
    }
};

CustomDialog的代码如下。

public class CustomAlertDialogFragment extends DialogFragment {

    public static CustomAlertDialogFragment newInstance(String title, String content) {
        CustomAlertDialogFragment customAlertDialogFragment = new CustomAlertDialogFragment();
        Bundle args = new Bundle();
        args.putString("title",title);
        args.putString("content", content);
        customAlertDialogFragment.setArguments(args);
        return customAlertDialogFragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString("title");
        String content = getArguments().getString("content");
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // title bar string
        builder.setTitle(title);
        builder.setPositiveButton(R.string.ok, null);

        builder.setMessage(content);
        AlertDialog errorDialog = builder.create();
        // return the Dialog object
        return errorDialog;
    }
}

如果用户同时移动到任何其他FragmentActivity,则不会显示该对话框。它应该抛出NullPointerException如果它没有得到上下文但它没有。什么可能是替代或解决方案。我已经提到其他SO问题,其中成员要求使用通知,但我的要求是对话。请帮忙。

1 个答案:

答案 0 :(得分:0)

我猜是Dialogue正在抛出一个名为window leaked的异常。在您的情况下,我认为您可能会考虑使用Application类。

无论如何,您可以在BroadcastReceiver课程中为您的Application课程注册Context,该课程将接收广播并按需显示对话框。

要检索getApplicationContext(),您可以使用public class MyApplication extends Application { // Declare the BroadcastReceiver in your Application class BroadcastReceiver myReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(firstTime) firstTime = false; boolean anyError = false; switch (getResultCode()) { case Activity.RESULT_OK: break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: case SmsManager.RESULT_ERROR_NO_SERVICE: case SmsManager.RESULT_ERROR_NULL_PDU: case SmsManager.RESULT_ERROR_RADIO_OFF: anyError = true; break; } sent.add(anyError); CustomAlertDialogFragment customAlertDialogFragment = CustomAlertDialogFragment.newInstance("Title",logMessage); customAlertDialogFragment.show(getApplicationContext().getSupportFragmentManager(), "TAG"); sent.clear(); } }; @Override public void onResume() { super.onCreate(); // Register the receiver here } @Override public void onPause() { super.onCreate(); // Un-register the receiver here } }

Application

要在清单中注册<application android:name="Yourpackage.MyApplication" android:allowBackup="false" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:logo="@drawable/your_logo" android:supportsRtl="true" android:theme="@style/AppTheme" tools:replace="android:icon"> <!-- Your activities --> </application> 课程,您需要定义名称

tsv