Jose Georges
Jose Georges

Reputation: 805

Difference between creating a class and extending fragment and just creating a fragment

I am relatively new at Android. I am taking a program on college and I am on my third semester. When we first started looking at fragments in the course, we always created them by right clicking the java folder and directly making a blank fragment. this would create the java file and the xml layout. then we would with help of the fragment manager, replace the content of the activity with the new fragment.

I thought this was the only way of doing it so far, but this week I've started to take a look at the developers course android offers on their website, and looking at the Creating a Fragment section, I learned that you can also just create a class and make it extend Fragment. Then, add an actual xml fragment tag to your layout and link your fragment to this xml.

My concerns about this are:

1) Is both ways doing the same process? 2) Is one of them better than the other? 3) Is there a moment when I'll prefer doing one or the other?

Upvotes: 1

Views: 216

Answers (1)

Abdul Wasae
Abdul Wasae

Reputation: 3688

There is no difference. Directly using the "Create a fragment" option is just a convenience option that does the following:

  1. Create a new class that extends from Fragment
  2. Create a new layout for your new custom fragment class
  3. Automatically set this layout as the contentView of your fragment (in the onCreate method)
  4. As a bonus, add some sample code for you to get started

You could do all these steps manually as well.

Upvotes: 3

Related Questions