mercredi 16 décembre 2015

correct implementation of MaterialDialog in DialogFragment

I'm implementing ProgressDialog that will be rotation proof, can be dismissed outside the class and should take content text as a parameter however after analysing samples and documentation I still have problem with rotations. What's the best pattern? I've tried to implement it as a singleton as well to pass content text.

public class ProgressDialog extends DialogFragment {

public static void show(FragmentActivity context) {
    ProgressDialog dialog = new ProgressDialog();
    dialog.show(context.getSupportFragmentManager(), "progress");
}

public static void dismiss(FragmentActivity context) {
    DialogFragment progressDialog = (DialogFragment) context.getSupportFragmentManager().findFragmentByTag("progress");
    if(context != null && progressDialog!=null) {
        progressDialog.dismiss();
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public void onDestroyView() {
    super.onDestroyView();

    if(this.getDialog() != null && getRetainInstance()) {
        getDialog().setDismissMessage(null);
    }
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new MaterialDialog.Builder(getActivity())
            .title("title")
            .content("content")
            .progress(true, 0)
            .progressIndeterminateStyle(false)
            .cancelable(false)
            .show();
}

}

Aucun commentaire:

Enregistrer un commentaire