Reputation: 6569
I have one Activity
and 5 Fragment
's. On the first Fragment
user inputs phone number. On the last(fifth) Fragment
I need to display this phone number, input other fields and send all the data to the server. For each Fragment
I'm using separate Presenter(MVP).
How to pass data from the first fragment to last?
1) I don't want to pass through all chain of fragments, because they don't need this data.
2) If store this data in the hosting Activity
than I need to call ((SomeActivity)getActivity()).getUserData()
inside Fragment
class and pass this data to Presenter.
3) I chose last approach. I've create singleton UserData class. This is simple POJO class. I'm creating instance of UserData
in the first Fragment
set needed data and than using it in the last Fragment
to retrieve data and add missing fields.
But I don't know is approach with singleton correct solution. Because I need to clear singleton instance when user goes to another Activity
.
If there are better solution?
EDIT
I thought about EventBus. But I will have to pass data from the Fragment to Presenter. Now I call UserData.getInstance()
inside present and retrieve data. I want code to be more elegant and also correct. I'm asking to hear opinion of more experienced developers what better to use.
Upvotes: 6
Views: 5440
Reputation: 2222
I've had lots of issues about passing the data to acitvity or fragment mutually. As a result, I made DataCache pass any object anywhere.
this is super easy example. Gihub : Android DataCache
Fragment for Sender
public class FragmentA {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String msg = "the phone number"
DataCache.getInstance().push(msg);
}
}
Fragment for Receiver
public class FragmentB {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String msg = DataCache.getInstance().pop(String.class);
}
}
try it :D I hope you to help.
Upvotes: 0
Reputation: 14755
I would do it the ModellViewPresenter way
class CustomerModel {
String phoneNumber
}
interface ICustomerOwner {
CustomerModel getCustomer()
}
class CustomerRegistrationWorkflowActivity extends Activity implements ICustomerOwner {
CustomerModel getCustomer()
...
}
class CustomerContactDataFragment // fragment1 where you edit the phone number.
class CustomerRegistrationSummaryFragment // fragment5 showing all data plus submit button)
Since the model lives in the (CustomerRegistrationWorkflow)Activity the fragments could communicate with the model by calling
((ICustomerOwner) getActivity()).getCustomer()
Upvotes: 2
Reputation: 2896
One calls elegant solution using event bus, the other - old good callbacks. My point of view is to avoid situation of "chain of fragments" - when one fragment directly calls another. If activity fully handles fragments replacement it has equal access to all 5 fragment. Therefore it can easily pass data between them using callbacks.
Don't see any problems with MVP in that case. Presenter use callback to send data to its view (Fragment). Fragment use callback to send data to its Activity. Activity then find 5th fragment in fragment manager and update/pass data in 5th fragment.
Situation with callback seems more robust than event bus, but still matter of taste.
Upvotes: 1
Reputation: 1261
A fragment is always connected to the Activity which contains it. So, it can communicate with the Activity easily. Using your Activity as a channel for communication or a medium for communication, you can create a communication link between the Fragments attached to the same Activity.
A fragment connected to an Activity can access the Activity's instance by getActivity() method and with this instance, fragment can perform tasks like calling methods of the Activity.
Now, to transfer data from one Fragment to another, you can create method in the Activity and store some data in a variable through that method. And in the other fragment, you can then access the value of that variable by help of other method. Concept of getter and setter methods can be used here.
For more info on Activity and Fragment's communication, visit the link: http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity
Upvotes: 0
Reputation: 12181
There are so many ways to send data from one class(Fragments as well) to another.
Considering Fragment's life cycle issues. You can use an Event bus for the same without any hassles.
class FragmentA {
Bus bus;
onCreate(){
bus = new Bus();
}
@Subscribe public void receiveMessage(String message) {
// TODO: Parse your message
}
onResume(){
bus.register(this);
}
onPause(){
bus.unregister(this);
}
}
class FragmentB {
onCreate(){
bus.post("You have a message");
}
}
More on integrating otto here. Or there are so many other choices as well. https://greenrobot.github.io/EventBus/
Upvotes: 2