Passing data between Fragments in View Pager

主宰稳场 提交于 2019-12-06 11:52:13
Shanto George

Create an interface

Implement that interface on the base activity where your fragment transaction

Call this interface from fragment1 while fragment transaction through a call back

Pass the value through this interface

For more details check this ans https://stackoverflow.com/a/32372333/5169060

You can try passing data between Fragments by making a common singleton class. Depending on your requirements you can create a class to set and get data. Below is one example of a singleton class which can be used to pass data between fragments

public class DataClass {
    private static DataClass dataObject = null;

    private DataClass() {

    }

    public static DataClass getInstance() {
        if (dataObject == null)
            dataObject = new DataClass();
        return dataObject;
    }
    private String distributor_id;;

    public String getDistributor_id() {
        return distributor_id;
    }

    public void setDistributor_id(String distributor_id) {
        this.distributor_id = distributor_id;
    }
}

After this set your data before you move to the next screen.

DataHolderClass.getInstance().setDistributor_id("your data");

and to get data

String data = DataClass.getInstance().getDistributor_id();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!