A Fragment
is a section of an Activity
, which has:
- its own lifecycle
- receives its own input events
- can be added or removed while the
Activity
is running.
A Fragment
must always be embedded in an Activity
.
Fragments
are not part of the API prior to HoneyComb (3.0). If you want to use Fragments
in an app targeting a platform version prior to HoneyComb, you need to add the Support Package to your project and use the FragmentActivity
to hold your Fragments
. The FragmentActivity
class has an API for dealing with Fragments
, whereas the Activity
class, prior to HoneyComb, doesn't.
If your project is targeting HoneyComb or newer only, you should use Activity
and notFragmentActivity
to hold your Fragments
.
Some details:
Use android.app.Fragment
with Activity
. Use android.support.v4.app.Fragment
withFragmentActivity
. Don't add the support package Fragment
to an Activity
as it will cause an Exception to be thrown.
A thing to be careful with: FragmentManager
and LoaderManager
have separate support versions for FragmentActivity:
If you are using a Fragment
in an Activity
(HoneyComb and up), call
-
getFragmentManager()
to getandroid.app.FragmentManager
-
getLoaderManager()
to getandroid.app.LoaderManager
if you are using a Fragment
in a FragmentActivity
(pre-HoneyComb), call:
-
getSupportFragmentManager()
to getandroid.support.v4.app.FragmentManager
. -
getSupportLoaderManager()
to getandroid.support.v4.app.LoaderManager