How do I start an activity from within a Fragment?

user1617134 picture user1617134 · Aug 22, 2012 · Viewed 131k times · Source

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.

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?

Answer

EkKoZ picture EkKoZ · Aug 22, 2012

You should do it with getActivity().startActivity(myIntent)