如何从 Fragment 中开始活动? [英] How do I start an activity from within a Fragment?
问题描述
我在 FragmentActivity
中有一组标签,每个标签都有自己的片段.当我尝试通过 onClickListener
并使用 startActivity(myIntent)
方法从该片段中启动新活动时,我的应用程序强制关闭.
I have a set of tabs inside of a FragmentActivity
that each hold their own fragment. When I tried to start a new activity from within that fragment via an onClickListener
, and using the startActivity(myIntent)
method, my application force closes.
环顾四周后,我找到了一两个对名为 startActivityFromFragment
的方法的参考,但在搜索了一个小时左右后,我找不到任何解释或示例说明如何使用它或者这是否是我应该使用的.
After looking around for a while, I found a reference or two to a method called startActivityFromFragment
, but after searching around for an hour or so I can't find any explanations or examples of how to use it or whether this is what I should be using.
我想我要问的是从活动启动新活动和从片段启动新活动之间是否有任何区别,如果有,我需要实现什么?
I guess what I'm asking is whether there is any difference between launching a new activity from an activity, and launching a new activity from a fragment, and if so, what do I need to implement?
推荐答案
你应该用 getActivity().startActivity(myIntent)
这篇关于如何从 Fragment 中开始活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!