Example summary of dialogfragment usage in Android Development

This article illustrates the use of dialogfragment in Android development. Share with you for your reference, as follows:

@H_ 404_ 3 @ background

Android officials recommend using dialogfragment instead of dialog, which can make it more reusable (reduce coupling) and more convenient (deal with screen flipping).

There are two ways to create a dialogfragment:

1. Override its oncreatedialog method - ① 2. Override its oncreateview method - ②

Although both methods can achieve the same effect, they have their own suitable application scenarios:

1. Method ① is generally used to create a scenario that replaces the traditional dialog dialog box, with simple UI and single function. 2. Method ② is generally used to create pop-up windows or full screen display scenes with complex content. The UI is complex and the function is complex. Generally, there are asynchronous operations such as network requests.

@H_ 404_ 3 @ application

Basic Usage

For method ①, create a dialog and return it:

Of course, you can also use a custom view to create:

PS: there are many ways to create a dialog, such as the following, which are slightly different. You should pay attention to:

For method ②, it is basically the same as the normal fragment usage:

Process screen flip

If we use the traditional dialog, we need to handle the screen flipping manually, but if we use the dialogfragment, we do not need to do any processing. The fragment manager will automatically manage the life cycle of the dialogfragment.

No title bar / full screen

In the basic usage, the code Notes have the place to set the theme. The following describes in detail how to set the untitled bar and realize the full screen in the following two methods:

No title block

For method ①:

For method ②:

Achieve full screen (width / height full screen)

Most common forms are as wide as the screen in width and adaptive in height. See the code directly below:

Method ①:

Line 12 of the code sets the background color of the window to be transparent, which is necessary;

Line 15 of code sets the width of the window to match_ Parent, the effect is as large as the screen width. Similarly, you can set the value of height. Set the width and height except match_ Parent and wrap_ Content can also be directly set to a specific value.

Method ②:

Line 14 of the code sets the background color of the window to be transparent, which is necessary;

Line 15 of the code sets all the padding values of the window to 0. This step is also necessary. The content cannot fill all the width and height.

Others are similar to those in ①.

Application scenario differences

At the beginning, the article briefly summarizes the application scenarios of method ① and method ②, which are described below:

1. From the basic usage, we can see that method ① provides a very convenient way to create a simple alternative dialog, which has advantages over method ②. 2. Method ① when using multithreading (such as network request), it cannot correctly obtain the status of the current fragment, resulting in null pointer exception. Method ② does not have this problem, and its creation method uses custom view by default, which is more convenient to deal with complex UI scenarios.

Here is an example: at the / / do something code of methods ① and ②, we perform some asynchronous operations:

If the current dialogfragment is closed before or just after the network request ends, a null pointer exception will be reported in line 19. The function of code 17 is to judge whether the current state of dialogfragment is available. However, isvisible() and ishide() cannot return its state correctly for method ①, and method ② is normal. Some states of dialogfragment created by methods ① and ② are not completely consistent.

PS: you can also use static handler combined with weak reference to handle asynchronous operations, so it is not important to obtain the dialogfragment state.

@H_ 404_ 3 @ summary

The above is a personal summary and views in the actual development process. I hope you can try with a skeptical attitude. If there are new views or incorrect places in the article, please contact me.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>