2016-12-31 3 views
0

Ich habe eine App, in der eine Nachricht gesendet wird und ihr Bericht nach einiger Zeit durch BroadcastReceiver empfangen wird. Dieser Bericht wird dem Benutzer über den Dialog angezeigt, für den ich DialogFragment verwende, wie unten gezeigt.DialogFragment anzeigen, selbst wenn sich der Kontext ändert

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(); 
    } 
}; 

Der Code für CustomDialog ist unten.

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; 
    } 
} 

Wenn der Benutzer zu jedem anderen Fragment oder Activity mittlerweile ist der Dialog nicht angezeigt. Es sollte NullPointerException werfen, wenn es Kontext nicht aber seinen nicht bekommt. Was kann die mögliche Alternative oder Lösung sein? Ich habe andere SO-Fragen angesprochen, bei denen Mitglieder gebeten haben, stattdessen eine Benachrichtigung zu verwenden, aber meine Anforderung ist ein Dialog. Bitte helfen Sie.

Antwort

0

Die Dialogue wirft eine Ausnahme namens window leaked ich denke. In Ihrem Fall denke ich, dass Sie vielleicht an die Application Klasse denken.

Wie auch immer, Sie können die BroadcastReceiver in Ihrer Application Klasse für Ihren Fall registrieren, die die Übertragung erhalten und den Dialog auf Abruf zeigen wird.

Um das Context zu recen, können Sie getApplicationContext() verwenden.

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 
    } 
} 

Um die Application Klasse in Ihrem Manifest registrieren Sie den Namen

<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> 
definieren müssen
Verwandte Themen