Solution to the problem of fragment multi-layer nested method invocation

Generation and introduction of fragment

Android runs on a variety of devices, including small screen mobile phones, large screen tablets and even televisions. In view of the gap in screen size, in many cases, we first develop a set of app for mobile phones, then copy a copy, and modify the layout to adapt to the super large screen of flat-panel Shenma. Can't an app adapt to both mobile phones and tablets? Of course, it must be. Fragment appeared to solve this problem. You can regard fragment as an integral part of an activity interface, and even the activity interface can be completely composed of different fragments. What's more handsome is that fragment has its own life cycle and receives and processes user events, so it's not necessary to write a pile of event processing code of controls in the activity. More importantly, you can dynamically add, replace and remove a fragment.

Fragment lifecycle

Fragments must exist depending on activities, so the life cycle of activities will directly affect the life cycle of fragments. The figure on the official website well illustrates the relationship between the two life cycles:

The following describes how to solve the problem of method call in the case of multi-layer nesting of fragments

It may appear in the same activity

Object 1: a fragment

Object 2: a listview or a viewadapter in B fragment

Interaction: after an interface in B fragment obtains information (possibly network information, etc.), it passes it to a fragment, or a fragment will notify the interface under B fragment to make changes after handling the event.

Option 1

Call method through middleman activity

Get the a fragment object in the B fragment through the middleman activity and call the method in the a fragment

Disadvantages: when there are many layers, the acquisition of this kind of middleman becomes a multi-layer middleman, and the middleman needs to be handled many times

For example, when you want to call a method in the subclass object of the parent class of pager in the list of viewpager in B fragment

1. Subclass objects need to override this method

2. The method needs to be defined by the parent class

3. You need to find the subclass object in the list in the B fragment and call this method

4. You need to find the bfragment object in a fragment and call this method

It can be seen that the more complex the nesting method is, the more hierarchical relationships this method needs to deal with... Therefore, scheme 2 is adopted

Option 2

Interface mode

Define interfaces in a fragment

Here is a case in which the method of B fragment is called in afragment by defining an interface:

Bfragment only needs to implement the interface and rewrite the interface method to be called by a fragment

In many inexplicably nested classes in B fragment:

The above is the solution to the problem of fragment multi-layer nested method call introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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