Using a packet to transfer data between a fragment and another fragment example

I have 3 sherlockListFragments in my application. Each fragment has some editTexts, and in the last fragment there is a button that, when you click all the data in the first and second fragments, must be accessible and saved. I used the package to send data between fragments. with a simple example below, This is the code of my first snippet:

public class PersonalDataFragment extends SherlockListFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragmet_personal_data, container, false); return v; } @Override public void onCreate(Bundle savedInstanceState) { PersonalDataFragment fragment = new PersonalDataFragment(); Bundle bundle = new Bundle(); bundle.putString("y", "koko"); //any string to be sent fragment.setArguments(bundle); super.onCreate(savedInstanceState); } 

} and this is the code for the fragment that receives the text:

 public class WorkExpRefFragment extends SherlockListFragment { String myInt; @Override public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_workexp_ref, container, false); final EditText t = (EditText) view.findViewById(R.id.editText5); final Button generate = (Button)view.findViewById(R.id.button2); generate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub t.setText(myInt + "sadfigha"); } }); return view; } @Override public void onCreate(Bundle savedInstanceState) { Bundle bundle = this.getArguments(); if(getArguments()!=null) { myInt = getArguments().getString("y"); } super.onCreate(savedInstanceState); } 

}

Now I have zero in the third fragment, what should I do? thanks in advance

+6
source share
1 answer

It is normal that your code does not work. In the first snippet, all you do is create a new instance of PersonalDataFragment , and you will pass it a Bundle with the data. The problem is that although the fragment contains data in the Bundle , the fragment itself is not the one used by the application (it is not even attached to the Activity ). You also install a Bundle on an instance of PersonalDataFragment , but you are trying to access data in WorkExpRefFragment , which obviously will not work, since the two fragments do not have a direct connection.

A simple solution for what you want to do is let the Activity "save" the data for your fragments, since the Activity is available for all fragments. First, create two methods in the Activity containing three fragments:

 public void saveData(int id, Bundle data) { // based on the id you'll know which fragment is trying to save data(see below) // the Bundle will hold the data } public Bundle getSavedData() { // here you'll save the data previously retrieved from the fragments and // return it in a Bundle } 

Then your fragments will save their data as follows:

 public class PersonalDataFragment extends SherlockListFragment { // this will identify the PersonalDataFragment fragment when trying to save data public void static int id PERSONAL_ID = 1; //... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle bundle = new Bundle(); bundle.putString("y", "koko"); //any string to be sent YourActivity activity = (YourActivity) getActivity(); activity.saveData(PERSONAL_ID, bundle); } } 

To retrieve data in a WorkExpRefFragment fragment:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); YourActivity activity = (YourActivity) getActivity(); Bundle savedData = activity.getSavedData(); } 

Depending on how you used these fragments, this solution may not work. Also, keep in mind that the Bundle you submit as described above will not be saved for configuration changes.

+6
source

Source: https://habr.com/ru/post/944773/


All Articles