Why is it recommended to use only the default constructor to create a Fragment?

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
Why is it recommended to use only the default constructor to create a Fragment?

I am Amit Shekhar, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Join my program and get high paying tech job: amitshekhar.me

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

Today, in this blog we will discuss an Android Interview Question.

Question: Why is it recommended to use only the default constructor to create a Fragment?

It is one of the commonly asked questions during the Android Interview.

Let’s discuss the answer.

So, Whenever the Android Framework decides to recreate our Fragment for example in case of orientation changes. Android calls the no-argument constructor of our Fragment.

The reason why can’t it call the constructor with the argument is that Android Framework has no idea what constructor we have created. So it can’t.

Let’s look at the recommended way of creating the newInstance of Fragment:

Here is the code:

private const val EXTRA_PDF_ID = "EXTRA_PDF_ID"

fun newInstance(pdfID: Long): PdfFragment {
    val fragment = PdfFragment()
    val bundle = Bundle()
    bundle.putLong(EXTRA_PDF_ID, pdfID)
    fragment.arguments = bundle
    return fragment
}

Here this setArguments method is very important.

private const val EXTRA_PDF_ID = "EXTRA_PDF_ID"

fun newInstance(pdfID: Long): PdfFragment {
    val fragment = PdfFragment()
    val bundle = Bundle()
    bundle.putLong(EXTRA_PDF_ID, pdfID)
    fragment.arguments = bundle
    return fragment
}

We must notice that we are passing the Bundle inside it. So when we create the instance for the first time using this newInstance method. The Android Framework can extract the bundle and store it.

So, when in case of Orientation changes, the Android Framework recreates the new fragment using the no-argument constructor and can attach the bundle to the fragment as it has stored the bundle earlier.

And later again, we can access that data in our onCreate() method by using getArguments() like this.

val pdfID = arguments?.getLong(EXTRA_PDF_ID)

It means that when the system restores a fragment, it will automatically restore our bundle. And we will be able to restore the state of the fragment to the same state the fragment was initialized with.

So, now we know the answer to Why is it recommended to use only the default constructor to create a Fragment?

Watch the video format: Why is it recommended to use only the default constructor to create a Fragment?

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar

You can connect with me on:

Read all of my high-quality blogs here.