Communication between Activity and Fragments

Android how to communicate between Activity and Fragment

Often we need to communicate between Activity and Fragments. Fragment to Activity communication is done through interfaces.

In order to create a communication flow between Fragment and Activity you can define an interface in the Fragment class. This interface should be implemented in the Activity class. In Fragment the interface is initialised in onAttach. Once it is initialised the interface function can be called to send message to Activity via interface call.

Fragment To Activity Communication

1> Define interface in Fragment class

    public interface OnFragmentInteractionListener {
        void onFragmentInteraction(String msg);
    }

2> Initialise Interface in onAttach

    OnFragmentInteractionListener mListener;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

3> Implement Interface method in Activity

public class MainActivity extends AppCompatActivity implements DetailFragment.OnFragmentInteractionListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.container, new DetailFragment(), "DetailFragment").commit();

    }

    @Override
    public void onFragmentInteraction(String msg) {
        Log.i("tag", "Message from fragment:" + msg);
    }
}

4> Call interface method in Fragment.

    public void onButtonPressed(String msg) {
        if (mListener != null) {
            mListener.onFragmentInteraction(msg);
        }
    }

Activity To Fragment Communication

We saw how to implement interface method to communicate from fragment to activity. Now if there is a need to communicate from activity to fragment then we need to use fragment manager to call fragment method directly.

FragmentManager is used to get fragments attached to the activity. Once you get the reference to the fragment then it can be cast to call the method directly. Let’s see an example of how to call fragment method from the activity class.

1> Create method in Fragment class

    public void methodCallFromActivity(){
        Log.i("tag", "Method call directly from activity");
    }

2> Call Fragment method from Activity class

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.button2){
            FragmentManager fm = getSupportFragmentManager();
            DetailFragment f = (DetailFragment) fm.findFragmentByTag("DetailFragment");
            f.methodCallFromActivity();
        }
    }

If the fragment is defined in the layout then findFragmentById is used to find respective fragment.

3 Comments

  1. Dennis R August 13, 2019
    • condescending-archimedes August 22, 2019
  2. Dennis R August 22, 2019

Leave a Reply