2

If I add a Fragment instance to Activity A as follows...

getSupportFragmentManager()
  .beginTransaction()
  .add(R.id.fragment_container, myFragment, "MY_FRAGMENT_TAG")
  .commit();

... And then I start Activity B on top of Activity A. Is it possible (?) to get a handle on the previously committed Fragment instance in Activity B something as follows...

getSupportFragmentManager()
  .findFragmentByTag("MY_FRAGMENT_TAG");

... This returns null so I wonder if this is possible using the approach above or otherwise??

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
  • 1
    If I'm not mistaken, the FragmentManager handles things on an Activity level, ie what you see in Activity A does not exist in Activity B until you actually reproduce it there. Don't quote me on that though. :) – karllindmark Jan 07 '14 at 13:57
  • Dupe: http://stackoverflow.com/questions/6034186/retain-fragment-state-between-activities – greg7gkb Mar 30 '16 at 19:51

1 Answers1

4

It is not possible, you can use Fragments only in its FragmentActivity.

In SDK doc, it says A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity. A Fragment is closely tied to the Activity it is in, and can not be used apart from one. So, you can't.

Please read the reference document: https://developer.android.com/reference/android/app/Fragment.html

slhddn
  • 1,917
  • 1
  • 15
  • 21