[Android Notes] use DialogFragment to host dialog and Android dialogfragment
CommonAlertDialogIt is destroyed when switching between the horizontal and vertical screens. If there is data on the dialog, it will also be lost. The solution is to useDialogFragment.
UsedialogFragmentGenerally, two methods are required:
OnCreateView (LayoutInflater, ViewGroup, Bundle)
OnCreateDialog (Bundle)
If you want to customize the dialog style, you only need to rewriteonCreateView, Inject a custom view, and then callDialogFragment # show ()Method.
Here we do not need to customize, just need to hostAlertDialogSo we only need to rewriteonCreateDialogMethod. In this method, we needAlertDialog.BuilderBuild a dialog and return it. The dialog parameter can be passed throughsetArgumentsInjection. The Code is as follows:
Package com. taobao. dialogfragmentdemo; import android. app. dialog; import android. content. dialogInterface; import android. OS. bundle; import android. support. annotation. nonNull; import android. support. v4.app. dialogFragment; import android. support. v7.app. alertDialog; public class AlertDialogFragment extends DialogFragment {private static final String PARAM_TITLE = "title"; private static final String PARAM_CO NTENT = "content"; private static DialogCallback mCallback; public callback () {} public static callback newInstance (String title, String content, DialogCallback callback) {AlertDialogFragment instance = new alertdigalofragment (); bundle bundle = new Bundle (); bundle. putString (PARAM_TITLE, title); bundle. putString (PARAM_CONTENT, content); instance. setArguments (bundle); mCallback = Callback; return instance ;}@ NonNull @ Override public Dialog onCreateDialog (Bundle savedInstanceState) {Bundle params = getArguments (); AlertDialog. builder builder = new AlertDialog. builder (getActivity (); builder. setTitle (params. getString (PARAM_TITLE); // no non-null judgment is performed. Add builder as needed. setMessage (params. getString (PARAM_CONTENT); builder. setPositiveButton ("OK", new DialogInterface. onClickListener (){@ Override public void onClick (DialogInterface diich, int which) {if (mCallback! = Null) mCallback. onPostiveClick () ;}}); builder. setNegativeButton ("cancel", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {if (mCallback! = Null) mCallback. onNegativeClick () ;}}); return builder. show () ;}public interface DialogCallback {public void onPostiveClick (); public void onNegativeClick ();}}
It is easy to use:
AlertDialogFragment dialog = AlertDialogFragment. newInstance ("title", "this is the alertdialog managed by fragment", new AlertDialogFragment. dialogCallback () {@ Override public void onPostiveClick () {Toast. makeText (MainActivity. this, "OK", Toast. LENGTH_SHORT ). show () ;}@ Override public void onNegativeClick () {Toast. makeText (MainActivity. this, "cancel", Toast. LENGTH_SHORT ). show () ;}}); dialog. show (getsuppfrfragmentmanager (), "dialog ");
You can also useDialogFragment#showTo find the corresponding dialogFragment:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); Fragment prevDialog = getSupportFragmentManager().findFragmentByTag("dialog"); if(prevDialog != null) { transaction.remove(prevDialog); }