Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using bundle to pass data between fragment to another fragment example

I have 3 sherlockListFragments in my app. Every fragment has some editTexts and the last fragment has a button which when it's pressed all data in the first and second fragments should be accessed and stored. I used bundle to send data between fragments. with the simple following example, This is the code of my first fragment :

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 of 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 a null in the third fragment, what should i do ? Thanks in advance

like image 725
Fareed Avatar asked May 11 '13 16:05

Fareed


People also ask

How you can pass the data from one fragment to another fragment?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken fragments to pass the data between two fragments.

How pass data from one fragment to another fragment in Android using intent?

So to share a string between fragments you can declare a static String in Activity. Access that string from Fragment A to set the value and Get the string value in fragment B. 2. Both fragments are hosted by different Activities- Then you can use putExtra to pass a string from Fragment A of Activity A to Activity B.

Could two fragments can communicate directly?

To keep fragments self-contained, you should not have fragments communicate directly with other fragments or with its host activity. The Fragment library provides two options for communication: a shared ViewModel and the Fragment Result API. The recommended option depends on the use case.


1 Answers

It's normal that your code fails. In the first fragment all you do is create a new instance of PersonalDataFragment and you pass it a Bundle with the data. The problem is although fragment holds the data in the Bundle, the fragment itself is not the one used by the app(isn't even attached to the Activity). You also set the Bundle on a PersonalDataFragment instance but you try to access the data in a WorkExpRefFragment which will obvious not work as the two fragments don't have a direct connection.

One simply solution for what you want to do is to let the Activity "save" the data for your fragments as the Activity is available for all fragments. First create two methods in the Activity holding the 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 like this:

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 the data in the WorkExpRefFragment fragment:

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

Depending on how you used those fragments this solution might not work. Also, keep in mind that the Bundle you pass like above will not be retained for a configuration change.

like image 131
user Avatar answered Oct 06 '22 01:10

user