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 - How to pass data from activity to fragment

I have some problems passing data from an activity to fragments in it. I searched around but didn't find an answer which suit my situation well. I have 2 fragment class named CurrentFragment.java and HistoryFragment.java. I initialize them as tabs in an Activity.

    Tab tab = actionBar.newTab()
            .setText(R.string.tab_current)
            .setTabListener(new TaskitTabListener<CurrentFragment>(
                    this, "current", CurrentFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab()
            .setText(R.string.tab_history)
            .setTabListener(new TaskitTabListener<HistoryFragment>(
                    this, "history", HistoryFragment.class));
    actionBar.addTab(tab);

I was told to use setArguments in the Activity and getArguments in the fragments. But in this situation how do I get fragment objects in the Activity? I can't use getFragmentManager().findFragmentById() since the fragments are added programmatically.

Also, I find some posts saying that I may use getActivity() in fragments to access data in the Activity container, but for me it keep returning null. Does anyone has a working example of that?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

[EDIT] I've updated my answer to better respond to your question.

You can also retrieve fragments by tag with getFragmentManager().findFragmentByTag("tag"). Be careful though, if the tab has not been viewed yet the fragment will not exist.

CurrentFragment curFrag = (CurrentFragment)
    getFragmentManager().findFragmentByTag("current");
if(curFrag == null) {
    // The user hasn't viewed this tab yet
} else {
    // Here's your data is a custom function you wrote to receive data as a fragment
    curFrag.heresYourData(data)
}

If you want the fragment to pull the data from the activity have your activity implement an Interface defined by the fragment. In the onAttach(Activity activity) lifecycle function for fragments you get access to the activity that created the fragment so you can cast that activity as the Interface you defined and make function calls. To do that put the interface in your fragment like this (You can also make the interface its own file if you want to share the same interface among many fragments):

public interface DataPullingInterface {
    public String getData();
}

Then implement the the interface in your activity like this:

public class MyActivity extends Activity implements DataPullingInterface {
    // Your activity code here
    public String getData() {
        return "This is my data"
    }
}

Finally in your onAttach(Activity activity) method in CurrentFragment cast the activity you receive as the interface you created so you can call those functions.

private DataPullingInterface mHostInterface;
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if(D) Log.d(TAG, "onAttach");
    try {
        mHostInterface = (DataPullingInterface) activity;
    } catch(ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement DataPullingInterface");
    }
    String myData = mHostInterface.getData();           
}

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

...