My question is apart from the obvious inheritance differences, what are the main differences between Fragment and FragmentActivity? To what scenarios are each class best suited? I'm trying to get an understanding of why both of these classes exist...
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 not FragmentActivity to hold your Fragments.
Some details:
Use android.app.Fragment with Activity. Use android.support.v4.app.Fragment with FragmentActivity. 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 get android.app.FragmentManager
getLoaderManager() to get android.app.LoaderManager
if you are using a Fragment in a FragmentActivity (pre-HoneyComb), call:
getSupportFragmentManager() to get android.support.v4.app.FragmentManager.
getSupportLoaderManager() to get android.support.v4.app.LoaderManager
so, don't do
//don't do this
myFragmentActivity.getLoaderManager();
//instead do this:
myFragmentActivity.getSupportLoaderManager();
or
//don't do this:
android.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
//instead do this:
android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager()
Also useful to know is that while a fragment has to be embedded in an Activity it doesn't have to be part of the Activity layout. It can be used as an invisible worker for the activity, with no UI of its own.
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.
Look here for more details
Think of FragmentActivity as a regular Activity class that can support Fragments. Prior to honeycomb, an activity class could not supoprt Fragments directly, so this is needed in activities that use Fragments.
If your target distribution is Honeycomb and beyond you can extend off of Activity instead.
Also a fragment is to be considered as a 'sub-activity'. It cannot exist without an activity. Always think of a fragment as a sub-activity and you should be good. So the activity would be the parent and the fragment(s) the child kind of symbolic relationship.
a FragmentActivity is an ad-hoc activity that contains Fragment.
In these few words I have explain you one of the main important changes that, with android 3.0(HoneyComb), android team has inserted in the android sdk.
With these new kind of concept your pieces of code and layout becomes more flexible and maintainable. If you search on google there are a lot of examples.
Related
I am a bit of a noob attempting to pass data between a fragment (living inside a tab layout) and the activity that runs the fragment.
I have found a solution here, but I am unable to call the parent activity from the fragment.
Send data from activity to fragment in Android
Looking up multiple answers, they all say the same thing
CALL getActivity()!!
Call parent's activity from a fragment
It doesn't exist. I am using the Android.Support.V4.App.Fragment.
I can access a property this.Activity, but it's calling a random FragmentActivity when this is being hosted on an AppCompatActivity.
How can I access my hosting AppCompatActivity from the support Fragment?
I would suggest using viewmodels to pass data between activities and fragments
I have used it in my basic code replace activity by fragment activity,but i have not got any kind of error.Application has working.
As seen in the documentation:
FragmentActivity
Base class for activities that want to use the support-based Fragment
and Loader APIs.
Activity
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup)
Can see more at : http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
A fragment activity can contain fragments (as it's name implies). It's the support version of activities for older APIs, which want to use fragments.
A Fragment , introduced in Android 3.0 HoneyComb is a portion of a user interface inside an activity. You can have many fragments in you UI.
FragmentActivity is the base class you must extend in order to use fragments with the support library.
The nuance here is that an Activity can use Fragments from the native SDK (as long as you are targeting API 11+), while a FragmentActivity can use Fragments from the support library.
So, assuming API 11+, if you are using android.app.Fragments in your app, you can use Activity, but if you are using android.support.v4.app.Fragments in your app, then you must use FragmentActivity.
This question already has answers here:
What is the difference between Fragment and FragmentActivity?
(4 answers)
Closed 9 years ago.
I am new in Android programming. I want to do a Fragment, I have seen examples that uses Fragment and FragmentActivity, What's the difference between them and which cases should be used each one?
Thanks
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.
Read here Difference between Fragment And FragmentActivity
FragmentActivity is part of the google-support-v4 lib which is basically adds a Fragment support to system with OS under 2.3. So FragmentActivity is exactly as a simple Activity only it gives you the ability to add Fragment to it.
Fragment is an object that shares parts of the Activity life cycle and can be added as part of you UI to an Activity or FragmentActivity with it's logic. the beauty of Fragments is that can be reused across different Activities in your application.
I am using FragmentStatePagerAdapter to create view pager for a list of objects. Each page is a fragment.
However, on that page, I am also using fragments to display some other data.
I got problem when doing this. I wonder can I put fragments in fragment. Or any other solutions to work this out?
Nested fragments are not supported by current fragment implementation (it was answered by Diane, Android engineer as well here:
Nested fragments are not currently supported. Trying to put a fragment
within the UI of another fragment will result in undefined and likely
broken behavior.
But it does not mean it is not doable - it can be achieved, however requires writing some more code than just fragment. There's comment in same thread by other user:
I managed this by extending FragmentActivity, FragmentManager, and
FragmentTransaction. Basic premise is extend DeferringFragmentActivity
in my activities, providing same api so no other code changes. When I
call getFragmentManager, I get an instance that
DeferringFragmentManager, and when I call beginTransaction, I get a
DeferredTransaction. This transaction stores POJOs with the called
method and arguments. When commit is call, we look for any pending
DeferredTransactions first. Once all transactions have been committed,
we start a real transaction and run all the stored methods with args
In general - unless you are desperated, just redesign your layout.
Fragments cannot hold other fragments.
It's not supported (errors with state)
I have used Fragments inside an Fragment in a project. The way I ended up doing is adding these inside fragments via code and using the fragment attached to the activity as a "proxy" for the other, so on the oncreateview() of the fragment you instantiate the inside fragments, ondestroyview() you remove the inside fragments and so on.
I was working on fragments and came across two things Activity and FragmentActivity which are used several times. I want to know that is there any difference between these two, because when I changed Activity with FragmentActivity, it had no effect on the app.
A FragmentActivity is a subclass of Activity that was built for the Android Support Package.
The FragmentActivity class adds a couple new methods to ensure compatibility with older versions of Android, but other than that, there really isn't much of a difference between the two. Just make sure you change all calls to getLoaderManager() and getFragmentManager() to getSupportLoaderManager() and getSupportFragmentManager() respectively.
FragmentActivity is part of the support library, while Activity is the framework's default class. They are functionally equivalent.
You should always use FragmentActivity and android.support.v4.app.Fragment instead of the platform default Activity and android.app.Fragment classes. Using the platform defaults mean that you are relying on whatever implementation of fragments is used in the device you are running on. These are often multiple years old, and contain bugs that have since been fixed in the support library.