Instantiate your Fragment by using a static factory method

From the Android developer documentation:

A Fragment represents a behavior or a portion of user interface in an Activity…[y]ou can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running.

Fragments were added in API 11 (Honeycomb)[1] in February of 2011[2] and have become a staple in most applications developed since. Typical usage cases of fragments necessitate dynamic variables to be present when the Fragment is instantiated. The most common method to achieve this is using a static factory method within the Fragment itself.

Using the static factory method

For the sake of simplicity, let’s say you have an Activity (ActivityMain) which adds a Fragment (TextFragment) to a container within the activity’s layout to display a particular String. Since the only job of the TextFragment is to display text, we will most likely call the fragment’s setArguments() method every time we want to use it.

TextFragment textFragment = new TextFragment();
Bundle bundle = new Bundle();
bundle.putString("arg_text", "Hello there!");

There is nothing inherently wrong with using the above method to instantiate the TextFragment; however, let’s add the static factory method to the TextFragment and examine the resulting code that utilizes it.

public class TextFragment extends Fragment {
    private static final String ARG_TEXT = "arg_text";

    public static TextFragment newInstance(String text) {

        TextFragment textFragment = new TextFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_TEXT, text);
        return textFragment;


With the static factory method in place, we can now initialize the TextFragment with the following code:

TextFragment textFragment = TextFragment.newInstance("Hello there!");

The resulting use of the static factory method provides three critical improvements over the first method of instantiating the TextFragment. First, the static factory method reduces code by subsequently increasing code readability. The benefits of this should be self-evident. Second, the static factory method ensures the same key is used to add the text to the Bundle. Although this can be alleviated by using a public static final String as a key, having multiple parameters only increases the potential to accidentally mix up keys and their accompanying parameters. Finally, the static factory method helps prevent mundane code errors such as forgetting to call setArguments(). In this respect, reusing the same code to instantiate a Fragment may significantly reduce the time it takes to debug such issues.


The benefits of using the static factory method to initialize your fragments may not be substantial enough to change your existing code; however, it is my humble opinion that it is in every developers’ best interest to adopt specific coding practices that have the potential to ease his/her workload. Practically speaking, the static factory method just makes more sense.

Further reading

A succinct blog post by Alex Lockwood.

A discussion on StackOverflow.

The Android developer documentation.


[1] <Accessed on February 16, 2015>

[2] <Accessed on February 16, 2015>


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s