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?
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
So, to pass data from the MotherActivity to such a Fragment you will need to create private Strings/Bundles above the onCreate of your Mother activity - which you can fill with the data you want to pass to the fragments, and pass them on via a method created after the onCreate (here called getMyData()).
Copy the code from the onCreate() method from the activity and paste it into the onCreateView() method of the fragment. To fix the errors, pass getActivity in place of this keyword and use getView().
To create a blank Fragment , expand app > java in Project: Android view, select the folder containing the Java code for your app, and choose File > New > Fragment > Fragment (Blank).
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With