I am developing an android application in which I need to launch an Fragment from the class which doesnot extend FragmentActivity . I am using support v4 package.
I will having the context in my current class. Can any one help me how to create FragmentManager object from current context?
public ToolBarGenerator(Context c)
{
context = c;
}
FragmentManager fm = (FragmentActivity)context.getSupportFragmentManager(); //getting error at this line.
//Called like the above
new ToolBarGenerator(getActivity())
If the
Contextyou have is from theFragmentActivity, you can just cast it toFragmentActivity, though I don’t know for sure if that’s your case.That said, it sounds like what you’re doing is bad practice. I would keep all Fragment transactions within the
FragmentActivityclass. If another class needs to request a differentFragmentshown, you should use callbacks or something similar.