Free Android app covering all aspects of addiction, including prevention and treatment

App remodelling: From Activities to fragments

Converting Android activities to fragments activityToFragment icon

Got an app using activities that you want to convert to using fragments?

We’ll show you how easy it is to convert activity apps to fragment apps. It’s no big deal.

Using fragments to rebuild your activities

Android configuration change and ragments icon

An activity is destroyed and recreated when a device’s configuration changes.

Usually the activity’s lifecycle takes care of saving and restoring its state. Sometimes you may need to restore lots of data. This could take some time resulting in a bad user experience.

Here’s where you can use fragments to retain an instance of your data. You can then get the data out of the fragment and rebuild your activity, super fast!

Be careful not to save objects that are tied to the activity; such as drawables, adapters, views or any other objects associated with a context. They’ll cause memory leaks – the object keeps a reference to the context (the activity for example), so it will remain in memory, leaving less for your app to use.

Have a look how easy it is to use a fragment to save an Object during a configuration change!

The parent's in control

fragments tut part3 icon

We only have one activity in our app. It hosts the fragments.

If you haven't already done so, have a look at :

We’re using the support library so make sure that you import the correct class:

fragment tut part2 icon

We'll build the two fragments in this part of the tutorial.

If you haven't already done so, have a look at :


fragmentsTut part1 icon

Our tutorial app has one activity and two fragments. One fragment contains a list and the other an image.

Running the app on a small device in portrait mode, displays the list fragment. Selecting an item in the list displays the second fragment containing an image.

Flipping the device to landscape displays both fragments side by side.

Running the app on a tablet displays the fragments side by side in both portrait and landscape mode.

Fragments: What you should know

Seriously, what are fragments?

fragments iconThink of components like buttons and images. You can reuse them over and over in different activities. You can also rearrange them within an activity.

Fragments are similar.

Fragments let you divide your activity into reusable components which have their own User Interface and lifecycle.

Android Fragments, Action Bar, Menus, Notifications and Tabs, aimed at the beginner Android developer, gives a detailed overview of these concepts and then shows you how they are applied in a fully functional application. Each concept is discussed in detail, using example code from the app, making it very easy to understand.

Learn how to use fragments in your applications, enabling your app to run flawlessly on large tablets and smaller mobile phones. Master using the Action Bar, menus, and navigation tabs. See how easy it is to send notifications, including the large picture and text notifications made possible in Jelly Bean.

What about Contextual Action Modes, popups, drop-down lists, Action Items and Custom Views? No problem, you’ll master them too!

Download the project files for free from the author’s website (www.101apps.co.za)! The application targets Jelly Bean and can run on Ice Cream Sandwich.

Available at Amazon.com and Kobo books

You can also download the application for free from the Amazon app store and Google Play