“To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity. The Fragment captures the interface implementation during its onAttach() lifecycle method and can then call the Interface methods in order to communicate with the Activity.” :- developer.android.com
Example
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="#00BBFF"> <fragment android:layout_width="match_parent" android:layout_height="wrap_content" android:name="com.example.myapp.tutorfragments2.FragmentA" android:id="@+id/fragment" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <fragment android:layout_width="match_parent" android:layout_height="wrap_content" android:name="com.example.myapp.tutorfragments2.FragmentB" android:id="@+id/fragment2" android:layout_below="@+id/fragment" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginTop="79dp" /> </RelativeLayout>
MainActivity.java
package com.example.myapp.tutorfragments2; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity implements Communicator { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void respond(String data) { FragmentManager manager = getSupportFragmentManager(); FragmentB f2 = (FragmentB) manager.findFragmentById(R.id.fragment2); f2.changeText(data); } }
FragmentA.java
package com.example.myapp.tutorfragments2; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; public class FragmentA extends Fragment implements View.OnClickListener { Button mButton; int mCounter = 0; Communicator mCommunicator; @Override public void onCreate(Bundle savedInstanceState) { //retrieving data from Saved instance when orientation changes super.onCreate(savedInstanceState); if (savedInstanceState == null) { mCounter = 0; } else { mCounter = savedInstanceState.getInt("mCounter", 0); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_a, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //creating communicater object with activity context //using this object we can call the respontd method implemented in activity mCommunicator = (Communicator) getActivity(); mButton = (Button) getActivity().findViewById(R.id.button); mButton.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: mCounter++; mCommunicator.respond("Counter val : " + mCounter); break; } } @Override public void onSaveInstanceState(Bundle outState) { //Saving data while orientation changes super.onSaveInstanceState(outState); outState.putInt("mCounter", mCounter); } }
fragment_a.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:background="#FFBB00"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:id="@+id/button" /> </LinearLayout>
FragmentB.java
package com.example.myapp.tutorfragments2; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FragmentB extends Fragment { TextView mTextView; String mData; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_b, container, false); if (savedInstanceState == null) { } else { mData = (String) savedInstanceState.get("text"); TextView myText = (TextView) view.findViewById(R.id.textView); myText.setText(mData); } return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mTextView = (TextView) getActivity().findViewById(R.id.textView); } public void changeText(String data) { this.mData = data; mTextView.setText(data); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("text", mData); } }
fragment_b.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:background="#99cc00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView" /> </LinearLayout>
Communicator Interface
package com.example.myapp.tutorfragments2; public interface Communicator { public void respond(String data); }
OutPut
ref : https://developer.android.com/training/basics/fragments/communicating.html