0

I have question about fragments updating values to each other.

I have a fragment called UserFragment and another called CarsFragment. UserFragment gets the user data and the carFragment gets multiple cars for the user owns. I then need to save this.

So, first, in the userFragment, I will have user details. Then, before I want to save the details, I will need to know if the car details has been populated/provided before saving the user data as a whole. Once provided, I can save it.

   public class UserFragemnt extends Fragment {


       private EditText mUserName;
       private EditText mUserOccupation;
       private String [] muserCars = null;   

       ...//onCreate
        mUserName= (EditText) view.findViewById(R.id.UserName)
        mUserOccupation= (EditText) view.findViewById(R.id.UserOccupation)

        if (userCar != null){   //so here i want to be sure the data is set by the carFragment
            //call saveUserdata()
        }

        //this function gets called by the main activity 
        public void getUserCarsString [] userCars){
            muserCars = userCars;
        }
}

So the question is, What is the best way for the carFrament to get me/set the data, so that when I go to save it, I have the data? So one thing to note in this question is, userFragment needs to wait on the data before saving.

I have read about having an interface, then defining it in the main activity. I did it as such with a conditional check to see if the car details are updated. I don't think this is the best way. I followed this example: http://developer.android.com/training/basics/fragments/communicating.html

Also briefly read about Binding data variable:http://developer.android.com/tools/data-binding/guide.html

Thanks

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Bobby_th
  • 91
  • 7

2 Answers2

0

This is not exactly the answer to your question but lets suppose you implemented communication between your fragments. Do you really want your fragments to be dependent on each other? I don't know the exact UX of your app but I would prefer each fragment to store its own data and other fragments (if they need to query this data) will load this data and rely on it. That way fragment does not care how the data it relies on was populated and if you will need to remove or add more fragments you will have minimal change (you will not maintain communication between specific fragments).

MikeL
  • 5,385
  • 42
  • 41
  • Make sense, But how will they know what data to query on? in my example UserFragment would need to know which car is associated with which car. So when querying the car sets they need to connect that to some user. So one of them need to pass an ID some how? – Bobby_th Jan 21 '16 at 23:23
  • In your case user unique id will be a good choice i think. To display a relevant user data in your UserFragment, it should be created with some unique user id, query some persistent data (shared prefs, db, file or whatever you are using) and show it. Any change will be saved back to the same place. Depending on your app/game CarFragment can be created with the same unique user id to load that user specific and other cars to choose from and save the choice. This way both fragments work with some storage and do not depend on each other. – MikeL Jan 22 '16 at 11:29
  • You can freely change the way you choose cars or edit user data and changing CarsFragment name will not effect UserFragment class. – MikeL Jan 22 '16 at 11:32
0

When attaching your Fragments to an Activity make sure to assign them their own unique tags.

getSupportFragmentManager()
  .beginTransaction()
  .add(R.id.content, new CarsFragment(), "CARS")
  .commit();

Once you have them you can reference one Fragment from the other using:

CarsFragment cars = (CarsFragment) getFragmentManager().findFragmentByTag("CARS");

You can now expose some additional method in your CarsFragment that will return any information you need inside the UserFragment - like getSelectedUserCar(). You can use a result of that method later on to decide whether you want to proceed with saving user's data.

Mateusz Herych
  • 1,595
  • 12
  • 20
  • Okay, so in this case, I don't need to implement an interface correct? I would just call it as another function from the UserFragment right? But is there way to call once i know that data is there. Like an event that tells the UserFragment that CarFragment has the data? Also i think this way might work with @Michael Liberman answer right. – Bobby_th Jan 21 '16 at 23:28
  • You don't need, it'll work without it, but it's usually a good practice to extract such interfaces. They let you decouple your fragments from each other and lead to a better architecture in general. – Mateusz Herych Jan 21 '16 at 23:47
  • One more question, what is the content ID in .add()? – Bobby_th Jan 26 '16 at 04:53
  • It's an id of a ```View``` you use to host your Fragments. E.g. ```R.id.content``` links to ```@+id/content``` in your layout xml file. – Mateusz Herych Jan 26 '16 at 07:54