Communication Between Two Fragments - Which Is Right Way?
I have read this manual and also i'm reading this book. developer.android.com says i should implement communication through activity. But the book says i can use setTargetFragment(
Solution 1:
setTargetFrament() and getTargetFrament() can be used in the context of one fragment that starts another fragment. The first fragment can pass its self as a reference to the second fragment:
MyFragmentnewFrag=newMyFragment();
newFrag.setTargetFragment(this, 0);
getFragmentManager().beginTransaction().replace(R.id.frag_one, newFrag).commit();
Now newFrag can use getTargetFrament()
to retrieve the oldFrag and access methods from oldFrag directly.
This is not however something that is recommanded to be used on an usual basis.
The recommanded way of communication between fragments is to be done through the parent activity, as the docs mention:
Often you will want one Fragment to communicate with another,
for example to change the content based on a user event.
All Fragment-to-Fragment communication is done through the associated Activity.
Two Fragments should never communicate directly.
Here is a example of that:
the layout for the main activity:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent" ><FrameLayoutandroid:id="@+id/frag_one"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"/><FrameLayoutandroid:id="@+id/frag_two"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"/></LinearLayout>
the Activity:
publicclassMainActivityextendsActivity
{
privateMyFragment f1;
privateMyFragment f2;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle b1 = newBundle();
b1.putString("name", "Fragment One");
f1 = MyFragment.createNew(b1);//we create a new fragment instance
f1.setOnReceiveListener(newMyFragment.ReceiveListener()//we create a new ReceiveListener and pass it to the fragment
{
@Overridepublicvoidrecv(String str)
{
//f1 has sent data to the activity, the activity passes forward to f2
f2.send(str);
}
});
//we attach the fragment to the activitygetFragmentManager().beginTransaction().add(R.id.frag_one, f1, "frag_one").commit();
//we repeat the above process for the second fragmentBundle b2 = newBundle();
b2.putString("name", "Fragment Two");
f2 = MyFragment.createNew(b2);
f2.setOnReceiveListener(newMyFragment.ReceiveListener()
{
@Overridepublicvoidrecv(String str)
{
f1.send(str);
}
});
getFragmentManager().beginTransaction().add(R.id.frag_two, f2, "frag_two").commit();
}
}
The fragment layout:
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><Buttonandroid:id="@+id/frag_btn"android:layout_height="wrap_content"android:layout_width="match_parent"android:layout_alignParentTop="true"/><TextViewandroid:id="@+id/frag_txt"android:layout_height="match_parent"android:layout_width="match_parent"android:layout_below="@+id/frag_btn"android:textSize="10sp"/></RelativeLayout>
The fragment class:
publicclassMyFragmentextendsFragment
{
privateReceiveListener recv_list;
privateButton btn;
privateTextView txt;
//static factory function that creates new fragments publicstaticMyFragmentcreateNew(Bundle b)
{
MyFragment f = newMyFragment();
f.setArguments(b);
return f;
}
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment, container, false);
}
@OverridepublicvoidonViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
btn = (Button) view.findViewById(R.id.frag_btn);
txt = (TextView) view.findViewById(R.id.frag_txt);
//we retrieve the passed arguments (in this case the name)Bundle b = getArguments();
final String name = b.getString("name");
btn.setText(name);
btn.setOnClickListener(newView.OnClickListener()
{
@OverridepublicvoidonClick(View v)
{
if(null != recv_list)
{
//now we pass the data to the parent activity
recv_list.recv(name + " says hello!");
}
}
});
}
//the activity passes data to the fragment using this methodpublicvoidsend(String s)
{
txt.append(s + "\n");
}
//helper method that will set the listenerpublicvoidsetOnReceiveListener(ReceiveListener l)
{
recv_list = l;
}
//the declaration of the listenerpublicinterfaceReceiveListener
{
publicvoidrecv(String str);
}
}
Post a Comment for "Communication Between Two Fragments - Which Is Right Way?"