1

Can I use FragmentManager to replace fragments with activity?

Below is my code. I use fragment manager to replace a fragment. But now I have a class which extends FragmentActivity and then I want to use fragmentmanager to replace fragment with it.

Fragment fragment2 = new HostFragment();
FragmentManager fragmentManager2 = getFragmentManager();
fragmentManager2.beginTransaction().replace(R.id.content_frame, activity).commit();

Is it possible?

gunar
  • 14,660
  • 7
  • 56
  • 87
ajkala
  • 93
  • 1
  • 9
  • why? u can make that activity a fragment. – keshav Jan 09 '14 at 07:10
  • I'm using fragment manager to replace a fragment with another fragmnet . But as you know , fragment is not an activity and there is something we can not work with fragment so that i change fragment with fragment activity instead. And then the next problem is I can not using fragment manager to replace fragment with fragment activity anymore . And then I'm stuck . – ajkala Jan 09 '14 at 07:17
  • U can do anything in ur fragment if u have context of activity holding it. – keshav Jan 09 '14 at 07:20
  • @keshav here is exactly my problem http://stackoverflow.com/questions/21013031/put-a-tabhost-inside-a-fragment .This problem will be solved if i use fragment activity instead of fragment . But when i use fragment the new problem is i can not replace this by fragment activity anymore :( – ajkala Jan 09 '14 at 07:25
  • @keshav I updated here http://stackoverflow.com/questions/21013031/put-a-tabhost-inside-a-fragment – ajkala Jan 09 '14 at 07:29
  • @keshav my problem will be solved easily if i use fragment activity instead of fragment , but when i change , my above problem is raised :( – ajkala Jan 09 '14 at 07:33
  • please give me some suggestion . Nearly crazy for searching – ajkala Jan 09 '14 at 07:37
  • as i seen ur layout, I think u can use viewpager see following links that have viewpager librery https://github.com/JakeWharton/Android-ViewPagerIndicator http://viewpagerindicator.com/ – keshav Jan 09 '14 at 07:45
  • @keshav I viewed it source code and sadly , it it use fragment activity public class SampleTabsWithIcons extends FragmentActivity so it has no change with my problem – ajkala Jan 09 '14 at 07:56
  • why not just convert your fragment activity into a fragment? O.o – Daniel Bo Jan 09 '14 at 08:45
  • @DanielBo That is what i ask . How can i do it ? – ajkala Jan 09 '14 at 11:10
  • well, change the class it is extending, and fix the compiler errors. Nothing more i can do without code – Daniel Bo Jan 09 '14 at 11:12
  • @DanielBo Arcording to what I said in comment above , There is something we can not work with Frament and it must be Fragment Activity in my opinion :( , I try to put an tabshost inside the fragment but I cannot . here is my problem http://stackoverflow.com/questions/21013031/put-a-tabhost-inside-a-fragment . So I try another way and change it to fragment activity , It's solved , but when change to fragmentactivity problem is I can not replace a fragment with the fragment activity i have been created. That it . I'm stuck. – ajkala Jan 09 '14 at 12:07
  • cant try myself without code, but it is possible to add a tabhost to a fragment, did that somewhere, cant remember where xD – Daniel Bo Jan 09 '14 at 13:24

1 Answers1

0

No it's not possible. You can only replace fragments with other fragments as FragmentManager is handling only fragments.
If you have in the activity (ActivityA) that you want to add some content that you would like to have in current activity (ActivityB), then move that content in a separate fragment (ContentFragment), load that fragment in ActivityA and in ActivityB. That's the reason why fragments were introduced.

gunar
  • 14,660
  • 7
  • 56
  • 87
  • I'm using fragment manager to replace a fragment with another fragmnet .And then when i put some component into fragmemt like Tabhost I can not work with it because fragment is not an activity and there is something we can not work with fragment so that i change fragment with fragment activity instead. And then the next problem is I can not using fragment manager to replace fragment with fragment activity anymore . And then I'm stuck . – ajkala Jan 09 '14 at 07:21
  • here is exactly my problem . http://stackoverflow.com/questions/21013031/put-a-tabhost-inside-a-fragment This problem will be solved if i use fragment activity instead of fragment . But when i use fragment the new problem is i can not replace this by fragment activity anymore :( – ajkala Jan 09 '14 at 07:25
  • `TabHost` is a `ViewGroup` why can't you use it? – gunar Jan 09 '14 at 07:25
  • I don't get your idea ? – ajkala Jan 09 '14 at 07:27
  • Yeah ! I voteup beacause your answer is the best for my above question . But my problem is still there . Anyway , thanks for your reply . – ajkala Jan 09 '14 at 11:56