"Turn" Pro Android Learning Note (46): Dialog (3): Dialog Box Bullets dialog box

Source: Internet
Author: User

Directory (?) [-]

    1. Implementation of the Help prompt box
    2. Implement the Reload Frame
    3. Talk about Fragment manager again

The button Help for the prompt box will trigger a popup with a new helper box.

Implementation of the Help prompt box

The implementation of the Help box is simple, using the rewrite Oncreateview () method, click the button is execute dismiss (), close the dialog box.

The code does not repeat here. Dialog fragment is closed in two ways, one is to execute dismiss directly in dialog fragment (), let's take a look at the source fragment of Dialogfragment:

[Java]View Plaincopy
  1. /**
  2. * Dismiss the fragment and its dialog. If The fragment is added to the
  3. * Back Stack, "all" back "stack" state "up" and including this entry would
  4. * Be popped. Otherwise, a new transaction would be committed to remove
  5. * The fragment.
  6. */
  7. Public void Dismiss () {
  8. Dismissinternal (false);
  9. }
  10. void Dismissinternal (boolean allowstateloss) {
  11. if (mdismissed) {
  12. return;
  13. }
  14. mdismissed = true;
  15. Mshownbyme = false;
  16. if (mdialog! = null) {
  17. Mdialog.dismiss ();
  18. Mdialog = null;
  19. }
  20. mviewdestroyed = true;
  21. if (Mbackstackid >= 0) {
  22. Getfragmentmanager (). Popbackstack (Mbackstackid,
  23. fragmentmanager.pop_back_stack_inclusive);
  24. Mbackstackid =-1;
  25. } Else {
  26. Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();
  27. Ft.remove (this);
  28. if (allowstateloss) {
  29. Ft.commitallowingstateloss ();
  30. } Else {
  31. Ft.commit ();
  32. }
  33. }
  34. }

If the back stack has the dialog, pop it out, otherwise ft.remove (this); Ft.commit ();. The operation of the estimated pop also contains ft.remove () and Ft.commit (). Calling dismiss () triggers the Ondismiss () callback function. Tracking status, as follows.

Implement the Reload Frame

The relevant code for implementing a bullet box in Promptdialogfragment is as follows. Another way to turn off dialog is to control it through the fragment transaction.

public void OnClick (View v) {
... ...
Switch (V.getid ()) {
Case R.ID.BUTTON_HELP:
Fragmenttransaction ft = Getfragmentmanager (). BeginTransaction ();

/* If you do not execute remove (), prompt dailog is in the lower level, tracking status, the system will not enter the Ondismiss () state. The main consideration is the aesthetic problem, if the following prompt dialog box is larger than the Help box, the visual effect is not good. The left image below is the effect of remove () and the effect of remove () is not performed on the right.
For Dialog,container is 0 or null. */
Ft.remove (this);
/* Add the current promptdialogfragment to the fallback stack when the user presses the back key, or by pressing the close button of the Help box dismiss the Help box is, re-display the prompt box.

for the back stack processing, the system has a certain degree of intelligence. For example: Execute two times addtostackstack (), do not actually repeat the stack. For example: Comment out the Remove () statement, that is, the prompt box does not disappear, but below the help box, such as the right, because the prompt box exists, we do not need to type the prompt box to the back stack, but in the experiment to find whether there is addtobackstack () will not affect the results, The system is able to analyze the existence of objects without the need to press stacks. Did not go to the source code, guess through the mbackstackid to intelligent processing. */
Ft.addtobackstack (NULL);

Helpdialogfragment HDF = helpdialogfragment.newinstance (r.string.help_message);
/* The handling of fragment is done through the fragment transaction, as in the activity Bullet box, via Show () mode. Until then, we have added the current fragment to the back stack through transaction. */
Hdf.show (Ft,mainactivity.help_dialog_tag);

Break
... ...
}
}

The fragment disappears from the UI through Remove () and Addtobackstack (), when it is still available through the Fragment manager and fallback stack.

Talk about Fragment manager again

With the fragment manager or fragment transaction, we can control the dialog fragment in detail. Show () is to add Fragment,dismiss () in the manager to remove fragment from the manager. We can't start with Add () and then show (), so a fragment object can only be added to the manager once. If fragment is dismiss (), it will be removed from the manager and we can no longer get the fragment information through the manager. Therefore, if we want to preserve some of the State or information of the dismiss dialog, we need to save it outside of dialog, for example by using activity.

The example code covered in this blog post can be downloaded in Pro Android Learning: Dialog small example.

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (46): Dialog (3): Dialog Box Bullets dialog box

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.