Foushi
Foushi

Reputation: 375

Fragment A Notify Fragment B

I have a function1 in FragA, and a function2 in FragB What I want is : when FragA use function1, it should notify FragB to use functionB

So what is the easiest and good way to do it ?

Thanks

Upvotes: 0

Views: 194

Answers (3)

Pankaj Nimgade
Pankaj Nimgade

Reputation: 4549

following is the example for your question, well it's quite crude in code but will give you an idea about how you can implement your requirement.

your activity should look like this FragmentTestOneActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import test.spinner.one.FragmentA;
import test.spinner.one.FragmentB;

public class FragmentTestOneActivity extends AppCompatActivity implements FragmentA.MyEventMaker{

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


    }

    @Override
    public void sendData(String text) {
        Log.d("FragmentTestOneActivity", "Text: "+text);
        FragmentB.setData(text);
    }
}

and below is the layout for the above activity activity_fragment_test_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cf5f5b"
    android:orientation="horizontal"
    android:padding="10dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="some.fragment.one.FragmentTestOneActivity"
    tools:showIn="@layout/activity_fragment_test_one">

    <fragment
        android:id="@+id/fragment_one"
        android:name="test.spinner.one.FragmentA"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:layout_weight="0.5"
        tools:layout="@layout/fragment_" />

    <fragment
        android:id="@+id/fragment_two"
        android:name="test.spinner.one.FragmentB"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        android:layout_weight="0.5"
        tools:layout="@layout/fragment_b" />
</LinearLayout>

FragmentA.java

import android.content.Context;
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;

/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentA extends Fragment {

    private MyEventMaker myEventMaker;
    private Button button;

    public FragmentA() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        myEventMaker = (MyEventMaker)context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_, container, false);
        button = (Button) view.findViewById(R.id.FragmentA_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myEventMaker.sendData("something");
            }
        });
        return view;
    }

    public interface MyEventMaker{
        void sendData(String text);
    }
}

fragment_a.xml

<LinearLayout 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:gravity="center"
    android:background="#b1a2f3"
    tools:context="test.spinner.one.FragmentA">

    <Button
        android:id="@+id/FragmentA_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button" />
</LinearLayout>

FragmentB

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentB extends Fragment {

    private static TextView textView;

    public FragmentB() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_b, container, false);
        textView = (TextView)view.findViewById(R.id.FragmentB_textView);
        return view;
    }

    public static void setData(String text){
        Log.d("FragmentB","Text: "+ text);
        textView.setText(""+text);
    }
}

fragment_b.xml

<LinearLayout 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:background="#efabca"
    android:gravity="center"
    tools:context="test.spinner.one.FragmentB">

    <TextView
        android:id="@+id/FragmentB_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

Upvotes: 1

Sourabh
Sourabh

Reputation: 8482

1. Using an EventBus

Use an EventBus to send event to the other fragment. You can try Otto or EventBus

2. Using FragmentManager and public method (I don't like these)

As @Alexander suggested, you can get reference to the other fragment and call its method

3. Using Broadcasts

You can use a LocalBroadcast and attach a receiver in both fragments to call the method when they receive a broadcast.

Upvotes: 3

Alexander
Alexander

Reputation: 48272

From func1 find your B fragment using FragmentManager.findFragmentByTag (or byId) then call your function on that fragment.

Or you can use a 3rd party even bus such as Otto

Upvotes: 0

Related Questions