title: Android listener serialization
body: <p>I have <code>CustomDialog</code> class that extends <code>DialogFragment.</code>  I override <code>onCreateDialog</code> method, to get custom dialog i wanted.</p>
  <pre><code>@Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
      dialog = new Dialog(activity, styleId);
      view = activity.getLayoutInflater().inflate(layoutId, null);
      dialog.setContentView(view);
      if (listener != null) {
          listener.onViewInit(view, this);
      }
      return dialog;
  
  }
  </code></pre>
  <p>This is custom dialog creation code. After view is inflated, I call listener method <code>listener.onViewInit(view, this)</code>  of type <code>OnViewInitListener </code> which is interface and extends Serializable, to bind custom code to view (view texts, listeners and etc.) , so that on rotation i want lose my button press logic.</p>
  <pre><code>@Override
  public void onSaveInstanceState(Bundle bundle) {
      bundle.putInt(&quot;layoutId&quot;, layoutId);
      bundle.putInt(&quot;styleId&quot;, styleId);
      bundle.putSerializable(&quot;listener&quot;, listener);
      super.onSaveInstanceState(bundle);
  
  }
  
  public RsCustomDialog setOnListenerAssignment(OnViewInitListener listener) {
      this.listener = listener;
      return this;
  }
  </code></pre>
  <p>When I implement <code>OnViewInitListener</code> from Activity,  on orientation change things work as expected:
  <code>onCreateDialog</code> is called every time fragment is recreated, and ther are no parcel errors, but when I press applications history button (on rightmost)
  <a href="https://i.stack.imgur.com/57NZZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/57NZZ.png" alt="android buttons" /></a><br />
  <sub>(source: <a href="https://tr2.cbsistatic.com/hub/i/r/2014/02/12/b6b5b8de-1998-4dde-b36b-104480d6a76c/resize/620x485/6073f6a446c6cba233ff5dab797115cd/Android_UpBackButtons_FigB_021214.png" rel="nofollow noreferrer">cbsistatic.com</a>)</sub></p>
  <p>I get this error:</p>
  <pre><code>10-09 11:09:38.256: E/AndroidRuntime(24153): FATAL EXCEPTION: main
  10-09 11:09:38.256: E/AndroidRuntime(24153): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = base.RsCustomDialog$OnClickListener)
  10-09 11:09:38.256: E/AndroidRuntime(24153):    at android.os.Parcel.writeSerializable(Parcel.java:1279)
  10-09 11:09:38.256: E/AndroidRuntime(24153):    at android.os.Parcel.writeValue(Parcel.java:1233)
  10-09 11:09:38.256: E/AndroidRuntime(24153):    at android.os.Parcel.writeMapInternal(Parcel.java:591)
  10-09 11:09:38.256: E/AndroidRuntime(24153):    at android.os.Bundle.writeToParcel(Bundle.java:1627)
  10-09 11:09:38.256: E/AndroidRuntime(24153):    at android.os.Parcel.writeBundle(Parcel.java:605)
  10-09 11:09:38.256: E/AndroidRuntime(24153):    at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:133)
  </code></pre>
  <p>I guess this is because, when I implement <code>OnViewInitListener </code> from my activity, java implicitly puts activity variable in implemented object, and Parcel can't handle Activity parcelation.</p>
  <p>Can anyone suggest how to deal with this problem, or advice a better solution.</p>