How to pass serialized object between 2 fragments using listener?
Answers
public class AlertDialogFragment extends DialogFragment {
private static final DialogInterface.OnClickListener DUMMY_ON_BUTTON_CLICKED_LISTENER = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
};
public static final class Builder implements Parcelable {
public static final Creator<Builder> CREATOR = new Creator<Builder>() {
@Override
public Builder createFromParcel(Parcel source) {
return new Builder(source);
}
@Override
public Builder[] newArray(int size) {
return new Builder[size];
}
};
private Optional<Integer> title;
private Optional<Integer> message;
private Optional<Integer> positiveButtonText;
private Optional<Integer> negativeButtonText;
public Builder() {
title = Optional.absent();
message = Optional.absent();
positiveButtonText = Optional.absent();
negativeButtonText = Optional.absent();
}
public Builder(Parcel in) {
title = (Optional<Integer>) in.readSerializable();
message = (Optional<Integer>) in.readSerializable();
positiveButtonText = (Optional<Integer>) in.readSerializable();
negativeButtonText = (Optional<Integer>) in.readSerializable();
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeSerializable(title);
out.writeSerializable(message);
out.writeSerializable(positiveButtonText);
out.writeSerializable(negativeButtonText);
}
@Override
public int describeContents() {
return 0;
}
public Builder withTitle(Integer title) {
this.title = Optional.fromNullable(title);
return this;
}
public Builder withMessage(Integer message) {
this.message = Optional.fromNullable(message);
return this;
}
public Builder withPositiveButton(int buttonText) {
this.positiveButtonText = Optional.fromNullable(buttonText);
return this;
}
public Builder withNegativeButton(int buttonText) {
this.negativeButtonText = Optional.fromNullable(buttonText);
return this;
}
private void set(AlertDialog.Builder dialogBuilder, final AlertDialogFragment alertDialogFragment) {
if (title.isPresent()) {
dialogBuilder.setTitle(title.get());
}
if (message.isPresent()) {
dialogBuilder.setMessage(message.get());
}
if (positiveButtonText.isPresent()) {
dialogBuilder.setPositiveButton(positiveButtonText.get(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialogFragment.onPositiveButtonClickedListener.onClick(dialog, which);
}
});
}
if (negativeButtonText.isPresent()) {
dialogBuilder.setNegativeButton(negativeButtonText.get(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
alertDialogFragment.onNegativeButtonClickedListener.onClick(dialog, which);
}
});
}
}