Activity and fragment are two ways to transfer data in Android

1. The first and most commonly used method is to use bundle to pass parameters

Then, in the oncreatview method in the fragment, get the bundle object through the getsegments () method, and then get the value we passed through through the key value of getString.

2. The second method is to define a method in the host activity, pass the value to be passed to the fragment, and obtain the value in the onattach method in the fragment.

3. The following is an extension to create fragments and pass values

If we do not need to pass values, we can directly create a fragment in the host activity as usual. However, if we need to pass data, we can use the newinstance (data) method. This method is self-defined, but it is a static method defined in the fragment.

In the host activity, create a fragment

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>