I have CustomDialog
class that extends DialogFragment.
I override onCreateDialog
method, to get custom dialog i wanted.
@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;
}
This is custom dialog creation code. After view is inflated, I call listener method listener.onViewInit(view, this)
of type OnViewInitListener
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.
@Override
public void onSaveInstanceState(Bundle bundle) {
bundle.putInt("layoutId", layoutId);
bundle.putInt("styleId", styleId);
bundle.putSerializable("listener", listener);
super.onSaveInstanceState(bundle);
}
public RsCustomDialog setOnListenerAssignment(OnViewInitListener listener) {
this.listener = listener;
return this;
}
When I implement OnViewInitListener
from Activity, on orientation change things work as expected:
onCreateDialog
is called every time fragment is recreated, and ther are no parcel errors, but when I press applications history button (on rightmost)
(source: cbsistatic.com)
I get this error:
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)
I guess this is because, when I implement OnViewInitListener
from my activity, java implicitly puts activity variable in implemented object, and Parcel can't handle Activity parcelation.
Can anyone suggest how to deal with this problem, or advice a better solution.