Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

android - Pass data between two fragments without using activity

I want to pass data between two fragments without using activity and fragment activity.

I don't want to pass data between fragments using activity like this : Communicating with Other Fragments

Below is my scenario :

I have one Parent fragment and inside that there are two child fragments.Now my need is to pass data between these two fragments.How to achieve this?

I looked into this : Event Bus but not getting working example for fragments.

Is there any other alternative to pass data between fragments?

Any help will be appreciated.

Edited as per InnocentKiller's answer :

In FragmentOne , I have implemented :

    FragmentTwo = new FragmentTwo();
    Bundle bundle = new Bundle();
    bundle.putString("Hello", "My name is Siddharth");
    fragment.setArguments(bundle);

In FragmentTwo, I have implemented :

    Bundle bundle = this.getArguments();
    String myInt = bundle.getString("Hello","Test");
    mStartTripButton.setText(myInt);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Best Way to exchange data between activity/fragments, fragment/fragment/, activity/activity, class/ class, make a common singleton class like:

public class DataHolderClass {
private static DataHolderClass dataObject = null;

private DataHolderClass() {
    // left blank intentionally
}

public static DataHolderClass getInstance() {
    if (dataObject == null)
        dataObject = new DataHolderClass();
    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;
 }
}

now set from anywhere(Fragment, activity, class) at any event before you move to new screen

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

now get anywhere(Fragment, activity, class)

 String _data = DataHolderClass.getInstance().getDistributor_id();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...