如何从活动片段传递数据片段、数据

2023-09-12 22:40:57 作者:凉兮* Armani

我将数据从一个活动的片段中有一些问题。我四处寻找,但没有找到它适合我的情况很好的答案。 我有一个名为2片段类 CurrentFragment.java HistoryFragment.java 。我初始化它们作为选项卡的活动。

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);

我被告知使用 setArguments 在片段的活性与 getArguments 。但是,在这种情况下,我怎么会在活动片段对象?我不能使用 getFragmentManager()。findFragmentById()由于片段编程方式添加。

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.

另外,我发现一些帖子说,我可以使用 getActivity()片段中访问数据的活动容器,但对我来说回头率空。有没有人有这方面的一个工作的例子?

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?

推荐答案

我已经更新了我的回答能够更好地回答您的问题。

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

您也可以通过检索标记片段, getFragmentManager()。findFragmentByTag(标签)。不过要小心,如果该标签还没有被观看尚未片段也就不存在了。

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)
}

如果你想片段拉从活动中的数据有你的活动落实片段定义的接口。在 onAttach(活动活动)生命周期功能的片段,您可以访问到创建的片段,所以你可以施放该活动为您定义的接口,使函数调用的活动。要做到这一点把接口的片段像这样(您也可以使接口它自己的文件,如果你想分享的碎片中相同的接口):

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"
    }
}

最后在CurrentFragment你的 onAttach(活动活动)方法施展活动收到你创建的,所以你可以调用这些功能。接口

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();           
}
 
精彩推荐
图片推荐