-4

I need a way to share Fragments through different activities, so for example I have MainActivity with a Fragment and when I go to SecondActivity I need that same Fragment loaded, but the Fragment can vary so it would not always be the same one.

I've guessed that I could get the actual Fragments id or tag and pass it on the Intent so I could retrieve it on SecondActivity and use it to load the correct Fragment, but I don't know how.

SpAiK
  • 3
  • 2
  • in SecondActivity you should instantiate a fragment the same way you do it in MainActivity. – Stan Apr 20 '15 at 18:45

1 Answers1

0

You can't. You have to create a new instance for the fragment and load again the data in it. If you created the fragment in a good way, it is a really simple task. The reasons why you can't reuse the fragment are the following:

  • If you could do something like that, what would happend to this fragment's lifecycle? What about going back from current activity to the other? Everything can be messed up.
  • Every activity has its own FragmentManager and they are never shared between activities, so you can't ask in another's activity for a fragment that doesn't belongs to it.

If you have some doubts on how to pass data using intents between activities to tell the fragment what to load, have a look at this post.

Community
  • 1
  • 1
droidpl
  • 5,632
  • 3
  • 32
  • 46