[Android Notes] use DialogFragment to host dialog and Android dialogfragment

Source: Internet
Author: User

[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); }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.