[英]How to call activity from another fragment.?
I already know how to make a fragment in Android.我已经知道如何在 Android 中制作片段。 I want to open another activity in the same fragment on a button click event.我想在按钮单击事件的同一片段中打开另一个活动。 The button is inside a fragment class.该按钮位于片段 class 内。
How do I do that?我怎么做?
There are two possibilities depending on what you need:根据您的需要,有两种可能性:
All Fragment-to-Fragment communication is done through the associated Activity.所有 Fragment 到 Fragment 的通信都是通过关联的 Activity 完成的。 Two Fragments should never communicate directly.两个 Fragment 永远不应该直接通信。
Here are some tutorials with example这是一些带有示例的教程
EDIT: April 2013编辑:2013 年 4 月
I like @AlexLockwood's comment.我喜欢@AlexLockwood 的评论。
In the case that one fragment starts another fragment, it's fine to just use setTargetFragment(Fragment, int)
and communicate with it directly by calling getTargetFragment()
.在一个片段开始另一个片段的情况下,只需使用setTargetFragment(Fragment, int)
并通过调用getTargetFragment()
直接与其通信就可以了。 Communicating with Fragments through the activity is usually a good idea because it keeps your fragments loosely coupled from one another.通过 Activity 与 Fragments 通信通常是一个好主意,因为它使您的 Fragment 彼此之间保持松散耦合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.