Interaction between fragment and activity in Android (two implementations)

(backgound is not set for the layout of the fragment)

About the concept and creation method of fragment in Android, I specially wrote a blog post "explain the two creation methods of fragment in Android" to make a detailed analysis on how to create the hybrid layout of fragment. Today, I will talk in detail about how to realize data interaction between fragment and host activity.

We can understand that in order to realize information interaction between fragments in the host activity, it must be through the host activity. It is impossible to realize information interaction directly between fragments.

For the data interaction between fragment and fragment, or between fragment and activity (directly deploy the layout contained in some fragments in the activity), I personally summarized two implementation methods:

(1) The data interaction between fragments is realized by transferring parameters through the bundle

(2) By defining a callback interface inside the fragment, you need the host activity to implement it. When the activity receives a callback through the interface, it can share information with other fragments in the layout if necessary.

Before starting the two implementation methods, I will introduce and demonstrate the effects of different settings of some personalization parameters. I have previously introduced that to manage fragments in the host activity, you must get the fragment Manager (fragment Management), and to implement operations (transactions) such as adding, deleting, changing fragments, you must enable the fragment transaction, Here we mainly demonstrate the different effects of using fragmenttransaction.add and replace before and after adding the fragmenttransaction.addtobackstack method and without setting the background for the fragment. For more detailed method introduction and usage, please refer to the API for details.

The demonstration effect of fragmenttransaction.addtobackstack method is not set (after opening multiple layers, press back to exit the program directly):

Set the fragmenttransaction.addtobackstack method to demonstrate the effect (there is no listener set here to listen to the changes of the background stack for judgment and processing):

It seems that there is no difference. In fact, when you press the back key, this exits one by one according to the opening order

Demonstration effect of using fragmenttransaction.add without setting background for fragment:

Note that it is emphasized here that the layout of the right fragment is not set with backgound. If backgound is set, the implementation effect is no different from that of replace, which is the main reason why there are so many timeouts today

We have seen the different implementation effects. Let's start to demonstrate the implementation code:

We use the layout file in the two methods of creating fragments in Android (adding fragments to the host activity through Java code). Please refer to the code of the layout file http://www.cnblogs.com/panhouye/p/6185093.html

(1) Pass parameters through bundle

Step 1: the Java code corresponding to the right fragment rightfragment.java:

Step 2: the Java implementation code corresponding to the main layout host activity mainactivity.java:

(2) Interface callback

Step 1: the Java implementation code of the left fragment leftfragment.java file

In this demonstration, the data interaction with the right fragment is triggered by clicking the button in the left fragment. Therefore, a callback interface needs to be added in this class to callback the method of modifying the text on the right in the host activity.

Step 2: the Java implementation code of the right fragment rightfragment.java file

The right side of this demonstration is a text display fragment. After clicking the button on the left side, the click event processing is reflected by changing the form of text. Therefore, the text modification method must be added to this fragment class.

Step 3: the Java implementation code mainactivity.java file of the host acvtivity of the main interface (in order to receive the fragment event callback, the host activity must implement the callback interface):

Summary:

The implementation method of interface callback in this demonstration seems to have a larger amount of code than that of using bundle to pass parameters, but in actual development, we are not only facing these two fragments, but the method of using interface callback to realize interactive data can better realize the reuse of fragment UI components and fundamentally solve the problem of large amount of code reuse, It is recommended that you master interface callback to realize data interaction.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

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
分享
二维码
< <上一篇
下一篇>>