Basic usage of dialogfragment
1. Create Dialogfragment
public class Dialoga extends Dialogfragment implements Dialoginterface.onclicklistener {
@Override
public Dialog Oncreatedialog (Bundle savedinstancestate) {
Alertdialog.builder Builder = new Alertdialog.builder ( Getactivity ());
Builder.setmessage (R.string.dialoga_title)
. Setpositivebutton (R.string.ok, this)
. Setnegativebutton ( R.string.cancel, this);
return Builder.create ();
}
@Override public
void OnClick (dialoginterface dialog, int id) {
switch (ID) {Case
Alertdialog.button_ NEGATIVE:
toast.maketext (Getactivity (), "NEGATIVE", Toast.length_short). Show ();
break;
Case alertdialog.button_positive:
toast.maketext (Getactivity (), "POSITIVE", Toast.length_short). Show ();
break;
Default: Break;}}}
Description: Customize a dialogfragment and rewrite its oncreatedialog () method.
2. Call the Dialogfragment
The following is the call to the Dialogfragment dialog box in fragmentactivity.
public class Dialogtest extends Fragmentactivity {
@Override public
void OnCreate (Bundle savedinstancestate) { C4/>super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
ShowDialog ();
}
private void ShowDialog () {
fragmentmanager fm = Getsupportfragmentmanager ();
Dialoga Dialoga = new Dialoga ();
DIALOGA.SHOW (FM, "Fragmenta");
}
customizing dialogfragment layouts
here's how to customize the layout of dialogfragment
Click to view: Custom dialogfragment Layout complete code
1. Set the layout file
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:layout_width=" Match_parent
" android:layout_height= "Match_parent"
android:orientation= "vertical" >
<textview
android: Layout_width= "Match_parent"
android:layout_height= "wrap_content"
android:hint= "@string/dialoga_intro" >
<imageview
android:id= "@+id/image"
android:layout_width= "Wrap_content"
android: layout_height= "Wrap_content"
android:src= "@drawable/ic_action_video"/>
</LinearLayout>
2. Use layout
public class Dialoga extends Dialogfragment implements Dialoginterface.onclicklistener {@Override public Dialog oncreatedialog (Bundle savedinstancestate) {Alertdialog.builder Builder = new alertdialog.b
Uilder (Getactivity ());
Layoutinflater inflater = getactivity (). Getlayoutinflater (); Builder.setview (inflater.inflate (R.layout.dialoga, NULL)). Setmessage (R.string.dialoga_title). SetPositiveButton (
R.string.ok, this). Setnegativebutton (R.string.cancel, this);
return Builder.create ();
@Override public void OnClick (Dialoginterface dialog, int id) {switch (ID) {Case alertdialog.button_negative:
Toast.maketext (Getactivity (), "Negative", Toast.length_short). Show ();
Break
Case AlertDialog.BUTTON_POSITIVE:Toast.makeText (getactivity (), "POSITIVE", Toast.length_short). Show ();
Break
Default:break; }
}
}
dialogfragment and activity Interaction
describes the methods for customizing dialogfragment and activity Interactions
Click to view: The complete code for Dialogfragment and activity interaction
1. Define the communication interface
to define the communication interface between them in Dialogfragment.
Public interface Noticedialoglistener {public
void Ondialogpositiveclick (Dialogfragment dialog);
public void Ondialognegativeclick (Dialogfragment dialog);
}
Use this instance the interface to deliver action events
Noticedialoglistener Mlistener;
Override the Fragment.onattach () method to instantiate the Noticedialoglistener
@Override public
Void Onattach (activity activity) {
Super.onattach (activity);
Verify The host activity implements the callback interface
try {
//instantiate the Noticedialoglistener So we can send events to the host
Mlistener = (noticedialoglistener) activity;
} catch (ClassCastException e) {
//The activity doesn ' t implement the interface, throw exception throw
new ClassCastException ( Activity.tostring ()
+ "must implement Noticedialoglistener");
}
2. Call this interface in Dialogfragment
@Override public
void OnClick (dialoginterface dialog, int id) {
switch (ID) {Case
Alertdialog.button_ POSITIVE:
//toast.maketext (Getactivity (), "Negative", Toast.length_short). Show ();
Mlistener.ondialogpositiveclick (dialoga.this);
break;
Case alertdialog.button_negative:
//toast.maketext (Getactivity (), "Positive", Toast.length_short). Show ();
Mlistener.ondialognegativeclick (dialoga.this);
break;
Default: Break
;
}
}
3. Implement the interface in the activity
public class Dialogtest extends Fragmentactivity
implements Dialoga.noticedialoglistener {
...
@Override public
void Ondialogpositiveclick (Dialogfragment dialog) {
Toast.maketext (this, Positive Callback ", Toast.length_short). Show ();
@Override public
void Ondialognegativeclick (Dialogfragment dialog) {
Toast.maketext (this, Negative Callback ", Toast.length_short). Show ();
}
Comparison of Dialog and Dialogfragment
from the point of view of code writing, dialog is simpler to use, but Google is recommended to use Dialogfragment (for Android version 3.0 below) Can be combined using the dialogfragment and fragmentactivity provided in the support package. Today, try to create a dialog box in both ways, and discover that dialogfragment really has a very good feature (changes in the phone configuration that cause the activity to be recreated, such as a spin screen, The Dialogfragment dialog box will be automatically rebuilt by the Fragmentmanager, but the dialog based on the dialog implementation is not able to do so.
Here are two pieces of instance code:
They use the same interface: (Dialog.xml)
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:layout_width=" match_parent "
android:layout_height=" match_parent "
android:o" rientation= "vertical" >
<imageview
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/ic_launcher"/>
</LinearLayout>
1. dialog box based on dialog implementation
public class Mainactivity extends activity {
private Button CLK;
Private Dialog Dialog;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_main);
CLK = (Button) Findviewbyid (R.ID.CLK);
Dialog = New dialog (this);
Dialog.setcontentview (r.layout.dialog);
Clk.setonclicklistener (New Onclicklistener () {
@Override public
void OnClick (View v) {
dialog.show ();
}
});
}
}
When we click on the button, the dialog box pops up (the Android logo), and when we rotate the screen, the activity is recreated, the entire activity interface is OK, and the dialog box disappears.
In addition, there is actually a problem, that is, in the Logcat will see the exception information: Android. Leaked.. window, this is because Android requires all dialog to be closed before the activity ends. After we spin the screen, the activity is rebuilt, and the code logic above does not take into account the State of the dialog box and whether it is closed.
The above code is then modified to:
public class Mainactivity extends activity {private Button CLK;
Private Dialog Dialog;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
CLK = (Button) Findviewbyid (R.ID.CLK);
Dialog = New dialog (this);
Dialog.setcontentview (R.layout.dialog);
Clk.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {dialog.show ();
}
}); The status of the User Recovery dialog box if (savedinstancestate!= null && savedinstancestate.getboolean ("Dialog_show")) Clk.performclick
(); /** * is used to save the state of the dialog box to restore/@Override protected void Onsaveinstancestate (Bundle outstate) {super.onsaveinstances
Tate (Outstate);
if (dialog!= null && dialog.isshowing ()) Outstate.putboolean ("Dialog_show", true);
else Outstate.putboolean ("Dialog_show", false); /** * Before the activity is destroyed, make sure the dialog box closes/@Override protected void OnDestroy () {Super.ondestroy ();
if (dialog!= null && dialog.isshowing ()) Dialog.dismiss ();
}
}
2. Dialogfragment-based dialog box
Using the same interface layout as the dialog above, this only shows a simple dialog box, so you only rewrite the Oncreateview method
public class Mydialogfragment extends Dialogfragment {@Override public View Oncreatev Iew (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View v = inflater.inflate (r.layout.dia
Log, container, false);
return v;
} public class Mainactivity extends Fragmentactivity {private Button CLK;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
CLK = (Button) Findviewbyid (R.ID.CLK); Clk.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {mydialogfragment MDF
= new Mydialogfragment ();
Fragmenttransaction ft = Getsupportfragmentmanager (). BeginTransaction ();
Ft.settransition (Fragmenttransaction.transit_fragment_fade);
Mdf.show (FT, "df");
}
}); }
}
These two pieces of code can do the same thing in the first way, where we don't care about the reconstruction of the dialog box, and whether the dialog box is closed before the activity is destroyed, all of which is managed by Fragmentmanager.
In fact, Dialogfragment also has the advantage of fragment that you can implement a rollback within an activity (because Fragmentmanager will manage a fallback stack)