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