"Android Notes" using Dialogfragment Managed Dialog

Source: Internet
Author: User

Normal AlertDialog in the screen switch will be destroyed, if the dialog above the data, will also be lost. The solution is to use dialogfragment.
There are dialogFragment typically two ways to use a carbon copy:

Oncreateview (Layoutinflater, ViewGroup, Bundle)
Oncreatedialog (Bundle)

If you want to customize the dialog style, just need to copy onCreateView , inject a custom view, and then call the dialogfragment#show () method.

Here we do not need to customize, only need to be managed AlertDialog , so we just need a replication onCreateDialog method. Inside this method we need to AlertDialog.Builder build a dialog and return, dialog parameters can be setArguments injected. The specific code is as follows:

 PackageCom.taobao.dialogfragmentdemo;ImportAndroid.app.Dialog;ImportAndroid.content.DialogInterface;ImportAndroid.os.Bundle;ImportAndroid.support.annotation.NonNull;ImportAndroid.support.v4.app.DialogFragment;ImportAndroid.support.v7.app.AlertDialog; Public  class alertdialogfragment extends dialogfragment{    Private Static FinalString Param_title ="title";Private Static FinalString param_content ="Content";Private StaticDialogcallback Mcallback; Public alertdialogfragment()    {    } Public StaticAlertdialogfragmentnewinstance(String title,string Content,dialogcallback callback) {Alertdialogfragment instance =NewAlertdialogfragment (); Bundle bundle =NewBundle ();        Bundle.putstring (Param_title,title);        Bundle.putstring (param_content,content);        Instance.setarguments (bundle); Mcallback = callback;returnInstance }@NonNull    @Override     PublicDialogOncreatedialog(Bundle savedinstancestate)        {Bundle params = getarguments (); Alertdialog.builder Builder =NewAlertdialog.builder (Getactivity ()); Builder.settitle (params.getstring (param_title));//Do not make non-null judgments, add as neededBuilder.setmessage (params.getstring (param_content)); Builder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intwhich) {if(Mcallback! =NULL) Mcallback.onpostiveclick ();        }        }); Builder.setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () {@Override             Public void OnClick(Dialoginterface Dialog,intwhich) {if(Mcallback! =NULL) Mcallback.onnegativeclick (); }        });returnBuilder.show (); } Public  interface dialogcallback {         Public void Onpostiveclick(); Public void Onnegativeclick(); }}

It's also easy to use:

Alertdialogfragment dialog = Alertdialogfragment.newinstance ("title","This is fragment hosted Alertdialog.",NewAlertdialogfragment.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 (Getsupportfragmentmanager (),"Dialog");

You can also DialogFragment#show find the corresponding dialogfragment by using the second parameter tag:

 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); Fragment prevDialog = getSupportFragmentManager().findFragmentByTag("dialog"); if(prevDialog != null) {      transaction.remove(prevDialog); }

"Android Notes" using Dialogfragment Managed Dialog

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.