Kodeco Forums

Android Fragments Tutorial: An Introduction

In this Android Fragments tutorial you will learn the fundamental concepts of fragments while creating an app that displays rage comics.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/745-android-fragments-tutorial-an-introduction

Howdy! Kudos for tutorial, but I see one issue: for I know you should define empty constructor because of possible InstantiationException. Explanation is here: android - Do fragments really need an empty constructor? - Stack Overflow

Actually you do not need to define one if you do not define any other constructors: the Java compiler will generate one for you as stated specifically in the tutorial:

First, because you did not define any constructors, the compiler automatically generates an empty, default constructor that takes no arguments. This is all that you should have for a fragment: no other constructors.

In the SO post that you mentioned, the poster states they have a “a fragment with a Constructor with multiple arguments”. This violates what I mentioned in the tutorial. In this case, the compiler will not generate the default constructor for you.

In fact, in the accepted answer of the SO post, the example Fragment class given by Chris Jenkins has no empty constructor. And the accepted answer focuses on the fact that you should not be generating constructors but using the newInstance pattern to pass information to Fragment creation.

There are also several comments in the answers that note the generation of an empty constructor for you as long as you do not define a non-empty constructor.

So yes, you need one but best practice is to not define any constructors which will lead to the compiler generating one for you.

1 Like

Hi, I have a question about android NavigationDrawer pattern.
Is it good just for Fragment, or I can go with Activity since there some modules should only have focus on their own logic, and using different single Activities would simplify logic complexity?

tl;dr Sure but depending on how your navigation is organized you might have to do some extra logic to ensure smooth closing of the drawer and navigation to the new activity.


You can use activities in lieu of fragments and I personally have done this at former projects. Yes, in a sense it does remove the need for you to manage fragments. However, if you want to have the same drawer navigation in both activities between which you are switching, that can be tedious.

If you want to navigate from one activity to another via the drawer, you run into some decisions to make about how to coordinate closing/opening the drawer and transitioning to the new activity.

If you simply start a new activity when you click on a navigation drawer item, the loading of the new activity will seem abrupt as you go from one activity with the drawer open to the next, presumably the drawer closed which will look choppy and is not the best UX.

What I did at the previous project was to hold the clicked item as state temporarily and wait for the navigation drawer to close before starting the new activity so that there wasn’t an abrupt change.

I suppose you could also remove any transitions between the activities and force the drawer open on the new activity but this may not work well either.

Hope this helps.

This is the best Android tutorial I found so far. I am an iOS Developer and I could tell this tutorial explained in excellent logic, concisely, explained everything question i have, no need to search myself.

As an iOS Developer, I am very confident to handle Activity and Fragment now. Thanks! Look forward to more tutorials from you.

this tutorial is really good… ,but i need help with something…i have been working in a project that i want to use webview with progessbar in the fragments how can i do that ? help me with that…

thanks in advance