Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the 'best practice' for changing from 'Activities' to 'Fragments' for Android applications?

I am writing an application for Android platform. The application is already almost written for 2.x version, but I want to make it also for Honeycomb version. As you know, Honeycomb has 'Fragments' rather than 'Activities' for tab target.

There is about 30 activities in my application and it must be changed to fragments. It could be very hard word for me, so I want to know the 'best practice' about doing it.

Thanks.

like image 311
John Cooper Avatar asked May 24 '11 11:05

John Cooper


People also ask

Why you should use fragments on your Android application?

Fragments introduce modularity and reusability into your activity's UI by allowing you to divide the UI into discrete chunks. Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer.

How do you go from activity to fragment?

Can I go from fragment to activity Android? 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.

Are fragments faster than activities?

Each tab will have a fragment to hold the products. The tabs could either held in activity or a fragment. I do find fragments a slightly faster than activities but really its not something you would really notice in most cases. Regardless if they was intended for speed or not they still seem/feel little quicker.


1 Answers

Not really an 100% answer, but just my personal experience so far (and please correct me, anybody, if I got something wrong - I'm still a beginner to Android):

I have the same situation atm and thought I could just re-use my activities and change the layout. E.g. I intended to show my main menu on the left side and all other activities on the right.

However, I soon realized that this isn't really working on Tablet. It just looked crappy and didn't even make sense as the Action bar pretty much replaced my main menu, if used properly. Also, the increased space really screams for a completely different layout. In my app, this will change the user experience completely, so I'll have to re-think the whole app.

On the other hand, stuff that I had to repeat again and again might now be replaced by "invisible" fragments with no UI. I haven't looked closer to this part of the Honeycomb API, but it sounds at least promising.

What I found a nice feature is that you now can "close" your app very easily. In my case I only have one Activity, so a simple "finish" gracefully quits the whole app. I know that this isn't the "Android way" to deal with app life cycle. However, while I don't miss the "close" button on my Android phone (even though some apps, especially games, offer one) I catch myself again and again looking for one on Honeycomb. (That's probably because the GUI reminds one of desktop applications.)

Anyway, long story short: I would recommend re-thinking you app for Tablet. You really have SPACE there, so it just looks strange if you don't use it properly. What this means for your app I don't know - it obviously depends on what kind of app it is. Some activities you'll probably can just use as fragments, some will merge with others, some might become dispensable.

So I guess you should decide first of all if your app really makes sense on Tablets. If not, you could just do the little "optimze" tricks (like applying the holografic theme and maybe new layouts/resources). For everything else I would wait for 4.0/Ice Cream Sandwich, as you will be able to use Fragments on both Smartphone and Tablet then.

like image 82
jellyfish Avatar answered Oct 05 '22 03:10

jellyfish