How to transfer data from one fragment to another fragment of android

One of the ways that I know is activity. We can send data from fragment to activity and activity to fragment. Is there another way.

+7
android fragment
source share
5 answers

Transferring data from one fragment to another Bundle will help.

 LifeShapeDetailsFragment fragment = new LifeShapeDetailsFragment(); // object of next fragment Bundle bundle = new Bundle(); bundle.putInt("position", id); fragment.setArguments(bundle); 

Then push/call next Fragments.

and the code for the following snippet:

 Bundle bundle = this.getArguments(); int myInt = bundle.getInt("position", 0); 
+14
source share

Quote from the docs

Often you need one piece to bind to another, for example, to change content based on a custom event. Data exchange between fragments and fragments is carried out through the activity associated with it. Two fragments should not communicate directly.

I suggest you follow the method in the docs and I have not tried other alternatives

For more information and an example chekc docs in the link below

http://developer.android.com/training/basics/fragments/communicating.html

+4
source share

There are two methods that I consider viable:

A. Report on your activity and forward messages to other fragments through this activity of the owner. Details on this can be found in the official documentation for Android:

http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Quote:

In some cases, you may need a snippet to share events using the Event. A good way to do this is to define a callback interface inside the fragment and require that host activity execute it. When an activity receives a callback through an interface, it can share information with other fragments in the layout as needed.

The communication interface may look something like this:

 public interface IActionListener{ //You can also add parameters to pass along etc public void doSomething(); } 

The snippet will look something like this:

 public class MyFragment extends Fragment{ private WeakReference<IActionListener> actionCallback; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception actionCallback = new WeakReference<IActionListener>((IActionListener) activity); } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement IActionListener."); } } } 

I use WeakReference here, but it really is up to you. Now you can use actionCallback to communicate with the property and call methods defined in the IActionListener.

Own activity will look like this:

 public class MyActivity extends ActionBarActivity implements IActionListener { public void doSomething(){ //Here you can forward information to other fragments } } 

B. Now, with regard to the second method , you can have fragments that directly communicate with each other using interfaces - this way you do not need to know the exact class of the fragment that you are talking to, which provides a free clutch.

The setup is as follows: you have two fragments (or more) and activity (to start the second fragment). We have an interface that allows Fragment 2 to send a response to fragment 1 after it is executed. For simplicity, we simply reuse interface I, defined in A.

Here is our snippet 1:

 public class FragmentOne extends Fragment implements IActionListener { public void doSomething() {//The response from Fragment 2 will be processed here} } 

Using the method described in A. Fragment 1 asks for an Activity action to start fragment 2. However, Activity will go through fragment 1 as an argument for fragment 2, so fragment 2 can later indirectly access fragment 1 and send an answer. Let's see as the prefix of Activity Fragment 2:

  public class MyActivity extends ActionBarActivity { // The number is pretty random, we just need a request code to identify our request later public final int REQUEST_CODE = 10; //We use this to identify a fragment by tag public final String FRAGMENT_TAG = "MyFragmentTag"; @Override public void onStartFragmentTwo() { FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); // The requesting fragment (you must have originally added Fragment 1 using //this Tag !) Fragment requester = manager.findFragmentByTag(FRAGMENT_TAG); // Prepare the target fragment Fragment target = new FragmentTwo(); //Here we set the Fragment 1 as the target fragment of Fragment 2 //communication endeavors target.getSelf().setTargetFragment(requester, REQUEST_CODE); // Hide the requesting fragment, so we can go fullscreen on the target transaction.hide(requester); transaction.add(R.id.fragment_container, target.getSelf(), FRAGMENT_TAG); transaction.addToBackStack(null); transaction.commit(); } } 

Tip . I use-Framework Support, so if you are developing exclusively for> Android 3.0, you can simply use FragmentActivity instead of ActionBarActivity.

FragmentTwo now begins, let's see how FragmentTwo will communicate with FragmentOne:

 public class FragmentTwo extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if(savedInstanceState != null){ // Restore our target fragment that we previously saved in onSaveInstanceState setTargetFragment(getActivity().getSupportFragmentManager().getFragment(savedInstanceState, TAG), MyActivity.REQUEST_CODE); } return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Retain our callback fragment, the TAG is just a key by which we later access the fragment getActivity().getSupportFragmentManager().putFragment(outState, TAG, getTargetFragment()); } public void onSave(){ //This gets called, when the fragment has done all its work and is ready to send the reply to Fragment 1 IActionListener callback = (IActionListener) getTargetFragment(); callback.doSomething(); } } 

The doSomething () implementation in fragment 1 will now be implemented.

+4
source share

Here is the solution

Follow the instructions below:

1 create such an interface

  public interface TitleChangeListener { public void onUpdateTitle(String title); } 

2. implements your activities using this interface

  for.eg public class OrderDetail extends ActionBarActivity implements TitleChangeListener 

3.In this activity, create on onUpdateTitle ()

  public void onUpdateTitle(String title) { //here orderCompletedDetail is the object second fragment name ,In which fragement I want send data. orderCompletedDetail.setTitle(title); } 

4.Now, In Fragment write some code.

  public class OrderPendingDetail extends Fragment { private View rootView; private Context context; private OrderDetail orderDetail; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.order_pending_detail, container, false); context = rootView.getContext(); //here OrderDetail is the name of ActionBarActivity orderDetail = (OrderDetail) context; //here pass some text to second Fragment using button ClickListener but_updateOrder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // here call to Activity onUpdateTitle() orderDetail.onUpdateTitle("bridal"); } }); return rootView; } } 

5. Enter some code in the second fragment of setTitle ()

  public void setTitle(String title) { TextView orderCompeted_name=(TextView)rootView.findViewById(R.id.textView_orderCompeted_name); orderCompeted_name.setText(title); //here you see the "bridal" value for TextView } 

In this solution, when you click the Snippet button, this time shows the value in the second snippet. Hope this will be helpful for you .. !!

+2
source share

Providing fragments with each other through actions as intermediaries is common good practice when using fragments. Visit http://developer.android.com/guide/components/fragments.html for more information on this important template. At any time when you need to interact with another fragment, you should always use the method in the activity of fragments, and do not directly contact another fragment. The only time it makes sense to refer to one fragment from another, you know that you do not need to reuse the fragment in another action. You should almost always write fragments, assuming you will reuse them, rather than hard code them with each other.

0
source share

All Articles