title: Activity crashes when dialogFragment is opened and onPause() is called
body: <p>I made a custom dialog (extends DialogFragment) that appears in several activities.</p>
  
  <p>If the activity comes to foreground while the dialog is opened, I get the following error:</p>
  
  <pre><code> FATAL EXCEPTION: main
  
  Process: package.name, PID: 11137
  
  java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = myFragmentOrActivityWhereOccuredTheError)
  ...
   Caused by: java.io.NotSerializableException: android.support.v7.widget.AppCompatButton
  </code></pre>
  
  <p>Depends on the activity or fragment, "Caused by" changes, because the problem is not there, is the DialogFragment <strong>(without showing the dialog fragments everything works)</strong></p>
  
  <p><strong>There is a solution:</strong> calling dismiss() before the activity goes to foreground. But I have to write a lot of code because I have to show it again in case the dialog was opened before the activity came to foreground and also is not simple to handle that with the complexity of the activies.</p>
  
  <p><strong>What I need:</strong> Solve the problem without dismissing the dialog. I think I have an error on my DialogFragment Class. So... this is the code of my class:</p>
  
  <pre><code>public class RequestDialog extends DialogFragment {
  
      public static String DIALOG_INTERFACE = "dialogInterface";
      public static String REQUIERE_ACTIVACION_MANUAL = "activationMode";
      public static String SCHEME = "package";
  
      public interface MyDialogInterface extends Serializable {
          void onClickContinuarEvent(int permisoRequerido);
          void onClickCancelarEvent(int permisoRequerido);
      }
  
      private MyDialogInterface callbackListener;
  
      /**
       * dialogInterface - instance of MyDialogInterface which will handle
       * callback events
       */
      public static RequestDialog getInstance(MyDialogInterface dialogInterface, boolean activationMode) {
          RequestDialog fragmentDialog = new RequestDialog();
  
          // set fragment arguments
          Bundle args = new Bundle();
          args.putSerializable(DIALOG_INTERFACE, dialogInterface);
          args.putBoolean(REQUIERE_ACTIVACION_MANUAL, activationMode);
  
          fragmentDialog.setArguments(args);
  
          return fragmentDialog;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          Bundle extras = getArguments();
          callbackListener = (MyDialogInterface) extras.getSerializable(DIALOG_INTERFACE);
          final boolean activationMode = extras.getBoolean(REQUIERE_ACTIVACION_MANUAL);
  
          View view = View.inflate(getActivity(), R.layout.rationale_dialog, null);
  
          TextView texDetalle = view.findViewById(R.id.texDetalle);
          TextView texContinuar = view.findViewById(R.id.texContinuar);
          TextView texCancelar = view.findViewById(R.id.texCancelar);
          ImageView imgCabecera = view.findViewById(R.id.imgCabecera);
  
  
          imgCabecera.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.ic_folder));
          Typeface typeFace = Typeface.createFromAsset(getActivity().getAssets(), "fonts/MyFont.ttf");
          texDetalle.setTypeface(typeFace);
  
          final AlertDialog.Builder requestDialogBuilder = new AlertDialog.Builder(getActivity(), R.style.NarrowDialog);
          requestDialogBuilder.setView(view);
  
          final AlertDialog dialog = requestDialogBuilder.create();
          dialog.setContentView(view);
          final Window window = dialog.getWindow();
          if(window != null){
              window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
              window.setGravity(Gravity.CENTER);
  
              dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
              Display display = getActivity().getWindowManager().getDefaultDisplay();
              Point size = new Point();
              display.getSize(size);
              dialog.getWindow().setLayout(size.x*70/100, WindowManager.LayoutParams.WRAP_CONTENT);
  
              texContinuar.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      if(activationMode){
                          goToPreferencesSystem();
                          callbackListener.onClickCancelarEvent(1);
                      }
                      else{
                          callbackListener.onClickContinuarEvent(0);
                      }
                      dialog.dismiss();
                  }
              });
  
              texCancelar.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      callbackListener.onClickCancelarEvent(2);
                      dialog.dismiss();
                  }
              });
          }
          setCancelable(false);
          return dialog;
      }
  }
  </code></pre>
  
  <p>As you can see, there is two callback that I have to handle in the activies.</p>
  
  <p>So... Do you have any idea hoy to solve this problem?</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>In any activity I have to implement the dialog like this:</p>
  
  <pre><code>MyActivity implements RequestDialog.MyDialogInterface
  </code></pre>
  
  <p>And then override the callbacks:</p>
  
  <pre><code>@Override
  public void onClickContinuarEvent(int request) {
  }
  
  @Override
  public void onClickCancelarEvent(int permisoRequerido) {}
  </code></pre>