Some other examples (and even documentation at the time of this writing) use the deprecated onAttach methods. Here is a completely updated example.

Notes
- You do not want the fragments to communicate directly with each other or with the task. This connects them with certain activities and makes reuse difficult.
- The solution is to create a callback listener interface that will be implemented in Activity. When a Fragment wants to send a message to another Fragment or its parent action, it can do this through the interface.
- It is normal for an Activity to communicate directly with its child fragments of public methods.
- Thus, Activity serves as a controller, passing messages from one fragment to another.
The code
MainActivity.java
public class MainActivity extends AppCompatActivity implements GreenFragment.OnGreenFragmentListener { private static final String BLUE_TAG = "blue"; private static final String GREEN_TAG = "green"; BlueFragment mBlueFragment; GreenFragment mGreenFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
GreenFragment.java
public class GreenFragment extends Fragment { private OnGreenFragmentListener mCallback; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_green, container, false); Button button = v.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String message = "Hello, Blue! I'm Green."; mCallback.messageFromGreenFragment(message); } }); return v; }
BlueFragment.java
public class BlueFragment extends Fragment { private TextView mTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_blue, container, false); mTextView = v.findViewById(R.id.textview); return v; }
XML
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <FrameLayout android:id="@+id/green_fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginBottom="16dp" /> <FrameLayout android:id="@+id/blue_fragment_container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout>
fragment_green.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#98e8ba" android:padding="8dp" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button" android:text="send message to blue" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
fragment_blue.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#30c9fb" android:padding="16dp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textview" android:text="TextView" android:textSize="24sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Suragch Oct 23 '17 at 2:48 on 2017-10-23 14:48
source share