android-fragment transfer data

Today, let's take a look at the transfer of data between fragment and fragment and the transfer of data with activity.
Here we use the interface method (learning Android requires the basis of java, like me who learns Android in a half-hearted way, it is irritating)

First build a simple interface, the top activity, the following two fragments
Insert picture description here

First, let's first look at the fragment passing data to the activity.

First come an interface

public interface getMyText {
    void getText(String msg);
}

Then the activity implements the interface and rewrites the abstract method. Assign values ​​to the control in the method and
Insert picture description here
then write the method in the fragment.
Insert picture description here
Run and click to see the effect, as shown in the figure:
Insert picture description here

Second, let's look at the communication between fragment and fragment

Implement the interface in the second fragment
Insert picture description here
and then use the tag to find the second fragment through the activity bridge in the first fragment.
Insert picture description here
Run, the results are as follows:
Insert picture description here

Guess you like

Origin blog.csdn.net/Willow_Spring/article/details/112639789