在片段与其容器活动之间传递数据 [英] Passing data between a fragment and its container activity
问题描述
如何在片段与其容器活动之间传递数据?是否有类似于通过意图在活动之间传递数据的东西?
How can I pass data between a fragment and its container activity? Is there something similar to passing data between activities through intents?
我读了这个,但没有太大帮助:
http://developer.android.com/guide/topics/fundamentals/fragments.html#CommunicatingWithActivity
I read this, but it didn't help much:
http://developer.android.com/guide/topics/fundamentals/fragments.html#CommunicatingWithActivity
推荐答案
在你的片段中你可以调用 getActivity()
.
In your fragment you can call getActivity()
.
这将使您可以访问创建片段的活动.从那里您显然可以调用活动中的任何类型的访问器方法.
This will give you access to the activity that created the fragment. From there you can obviously call any sort of accessor methods that are in the activity.
例如对于您的 Activity 上名为 getResult()
的方法:
e.g. for a method called getResult()
on your Activity:
((MyActivity) getActivity()).getResult();
这篇关于在片段与其容器活动之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!