Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android pass data between Fragments

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.

like image 994
Volodymyr Avatar asked Apr 05 '16 07:04

Volodymyr


People also ask

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

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 to create fragments in Android Studio?

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. Step 3 − Add the following code to src/MainActivity.java Step 4 − Create two fragments (FragmentOne and FragmentTwo) and add the following code −

How to communicate between fragments in the same fragment manager?

This way it helps to communicate between fragments in the same FragmentManager and also child to parent fragment or vice versa. To pass data between fragments in the same fragment manager, the listener should be added to the destination fragment with requestKey in order to receive the result produces from another fragment with the same key.

How do I pass a one-time value from one fragment to another?

In some cases, you may want to pass a one-time value between two fragments or between a fragment and its host activity. For example, you might have a fragment that reads QR codes, passing the data back to a previous fragment. In Fragment version 1.3.0 and higher, each FragmentManager implements FragmentResultOwner .


2 Answers

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()
like image 51
k3b Avatar answered Sep 19 '22 17:09

k3b


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/

like image 44
amalBit Avatar answered Sep 18 '22 17:09

amalBit