I've got a problem with fragments. I want to create different fragment files, which are connected and displayed in a framelayout inside my mainactivity. Also, it should be possible to switch the fragments with a bottom navigation view.
I created different fragment(blank) classes and they extend the class Fragment, but I'm not able to give these instances of this classes as a parameter to a method called setFragment(Fragment fragment).
Do you know what could cause this problem? My classes extend the class Fragment and so it should be possible to give my own created fragment classes, shouldn't it? +
Thanks for your help!
The solution to your problem is to make your fragments extend android.app.Fragment instead of Fragment. Everything should work fine after that.
However, you need to understand the difference between android.app.fragment and android.support.v4.app.Fragment.
If your app uses fragments and you want to support devices before API 11, you have to use android.support.v4.app.Fragment. However, if you're only targeting devices running API 11 or above, you can use android.app.Fragment.
There are two different kinds of fragments
android.app.Fragment
and
android.support.v4.app.Fragment
Use app fragments with android.app.FragmentManager
Use support fragments with android.support.v4.app.FragmentManager
It can be obtained by getSupportFragmentManager() of android.support.v4.app.FragmentActivity
Related
I am trying to use getChildFragmentManager() method on api 14, but of course I am getting error. Is there anyway to use this method on lower apis.
Thanks
To use nested fragments on API Level 16 and below, you need to use the fragments backport from the support-v4 or support-v13 portion of the Android Support package. This, in turn, requires you to inherit from FragmentActivity and have your fragments inherit from android.support.v4.app.Fragment. Then, you can call getChildFragmentManager() to use nested fragments.
To someone that appeared here by a search on Google
I was having a similar problem using DialogFragment
The problem was because I was importing android.app.DialogFragment instead of android.support.v4.app.DialogFragment
DialogFragment or not, be sure that you are importing the right libs :)
I'm using an nested fragment which is of class Fragment, as opposed to SupportFragment (v4).
I should use getChildSupportManager to handle this properly, but that suspects v4 support fragments, and I have a regular Fragment (which I can't change, it comes from a library).
How would I be able to solve this?
I should use getChildSupportManager to handle this properly, but that suspects v4 support fragments
AFAIK, there is no getChildSupportManager() method in Android. There is getChildFragmentManager().
If you are using getChildFragmentManager() from a FragmentActivity, you need to give it a fragment from the Android Support package backport (android.support.v4.app.Fragment). If you are using getChildFragmentManager() from a regular Activity on API Level 17+, you need to give it a native fragment (android.app.Fragment).
I have a regular Fragment (which I can't change, it comes from a library).
Then set your minSdkVersion to 17 and use Activity instead of FragmentActivity.
I am looking at fragments and its usage in Android. I am however getting confused as how do fragments work for all the different Android versions. What i mean is that we have this android.support.v4.app.Fragment for versions < Honeycomb and the fragment itself for Honeycomb and up. My question is that how does using either one of them affect the application in terms of efficieny and performance beside the backward compatibility. Also i keep seeing that the xml layout changes quite a bit depending on the support package that we use . Some have <fragment>, some have <fragmentActivity>, etc. How to know what is supposed to be used in the xml?
If someone can explain me in simple terms on how to go about using the fragment in different ways it would be highly appreciated.
To answer the first question there isn't any performance or efficiency issues with using either one of them beside the backward compatibility that you just mentioned.
For fragments that were added for API 11 and above there are three main classes:
android.app.Fragment
The base class for all fragment definitions
android.app.FragmentManager
The class for interacting with fragment objects inside an activity
android.app.FragmentTransaction
The class for performing an atomic set of fragment operations
And for fragments that were added for API 11 and below are:
android.support.v4.app.FragmentActivity
The base class for all activities using compatibility-based fragment (and loader) features
android.support.v4.app.Fragment
The base class for all fragment definitions
android.support.v4.app.FragmentManager
The class for interacting with fragment objects inside an activity
android.support.v4.app.FragmentTransaction
The class for performing an atomic set of fragment operations
If you are using the v4 support library then you need to make sure that you extend FragmentActivity by importing the android.support.v4.app.FragmentActivity and then in the xml you need to have a <fragment> tag with an android:name property where you define the complete class name.
More info: http://developer.android.com/training/basics/fragments/creating.html
I want to use Fragments in my application, but I cannot extend FragmentActivity because my application already extends an activity that is part of a library. Since this means that I cannot call getSupportFragmentManager(), I am looking for a workaround that would allow me to use Fragments without having to extend FragmentActivity. Is it possible?
Your library is going to need to extend the FragmentActivity.
I would be concerned about a library that requires you to use their base activities anyway.
As mentioned (where possible) grab the library source code and add it as a library project to eclipse and make its activities extend the FragmentActivity class.
I have created a tab fragments in android 2.2 , with a android compatibility support library , now in my application i have few activities some of them are extends Activity class and some of them extends ListActivity.
so how can i convert the existing Activity or ListActivity into Fragments so that i can take the advantage of Fragment features ?
As to create a fragment , one has to extends Fragment class but if an activity is deriving ListActivity then what to do to convert it in a fragment?
You need to review the Fragment documentation and samples on the Android Developers website. This will explain what a Fragment is able to do, and what you should be doing inside of your fragment.
In essence, its a very simple transition over to using Fragments once you have looked over the examples. You will need an Activity to contain the Fragments still.
To make this a lot simpler, I would advise you look into the ActionBarSherlock library, which will allow you to use the ActionBar and SupportLibrary back to 2.1.
To get you started, you will want to use the Fragment and ListFragment classes, which will be very similar to a standard activity, but the life cycles are a little different with a few naming changes.
You could try deriving it from ListFragment