Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

existing activity into fragments

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?

like image 992
Hunt Avatar asked Mar 19 '12 12:03

Hunt


People also ask

Can I put an activity 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.

How can you pass data from an activity to a fragment?

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()).

Can we convert a activity to fragment in Android?

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().

How do you create a fragment?

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).


2 Answers

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.

like image 119
Mimminito Avatar answered Sep 22 '22 02:09

Mimminito


You could try deriving it from ListFragment

like image 21
StevieB Avatar answered Sep 23 '22 02:09

StevieB