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
Related
I'm trying to implement custom DialogPreference, whose layout will use fragments. An app should support API 10 (which does not natively support fragments).
In order to work with fragments I need to get an instance of the android.support.v4.FragmentManager (normally usually being received from support FragmentActivity using getSupportFragmentManager).
I have no idea how to get android.support.v4.FragmentManager within DialogPreference. Any clue is appreciated.
If you are calling the dialog from an android.support.v4.app.FragmentActivity you should be able to do a type cast in your DialogPreference class, to get the Support-FragmentManager:
(android.support.v4.app.FragmentActivity)getContext()).getSupportFragmentManager()
... if that's not possible, another solution would be to launch an activity that looks like a dialog, and use your fragments there. How to open an activity from a PreferenceScreen?
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 throw a Dialog to the user of my app to confirm some action. From the developers API guides I learnt that they prefer using DialogFragments instead of dialog class.The activity which should show the dialog is a ListActivity and already has been coded.
My question is, if I need to use the DialogFragment then my activity may still extend ListActivity? Or I need to extend FragmentActivity (my app has min sdk 2.3.6) as I know android does not support multiple inheritence?
I dont want to use ListFragments
Thanks for any help.
I think you need to use the android-support-v4 library and then create a class that extends FragmentActivity but import this line android.support.v4.app.FragmentActivity; to support fragments from v2.3.6 and up. Change your ListActivity class to then extend ListFragment and invoke the class from your FragmentActivity. You can then use the DialogFragment with other fragments. This will change the whole structure of your project but it will force you to use better up to date api code.
Seeing that you don't want to use ListFragment I wouldn't go through all this trouble of converting all your classes into Fragments just to use DialogFragment you can just use normal Dialogs, they aren't a deprecated api so I wouldn't say there's anything wrong with them.
I think the reason DialogFragments are preferred is because they want you to use Fragments when developing your app, due to versatility for Tablet apps ect.
Hope this helps
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 am not sure if this is the right way to do it. I have a ChatActivity using FrameLayout in frame.xml. This activity needs to be reused across 5 activities. Is there anyway to do a code reuse? This acitivty runs independently of other activities.
For example, in activity A, which uses main.xml, I want ChatActivity and frame.xml to be included. What is the best way to achieve this besides merging the ChatActivity and frame.xml into activity A and main.xml? Merging the activities would mean that I have to copy and paste codes 5 times into different activities. I am not sure if this is the right way...
You cannot "include" an Activity to another Activity. Since your Activity has a basic functionality that all your other Activities use, you could have all your Activities extend this basic Activity.
The best way though for you would be to use Fragments and the compatibility library.
Regarding layouts, you can have reusable ones and import them to your current layout using include.
Hope this helps!
Use Fragments. you can create one Fragment and use the same in all activities. Fragments have their own view. Look at the following link for fragment.
http://developer.android.com/guide/topics/fundamentals/fragments.html
you can use fragments before 3.0 by including compatibility library.
http://android-developers.blogspot.com/2011/03/fragments-for-all.html
I android you can reuse xml files by using the include tag like
<include layout="#layout/okcancelbar_button" android:id="#+id/okcancelbar_ok"/>
To share functionalites of an activity with other activities, create a base activity with common functionalites and make the other activities extend from it.