-2

I am a beginner android developer, and right now I'm learning about fragments in android, and so and as I read that :

A fragment is just a part of an activity. You can have a fragment that takes up part of a screen or a whole screen. Or you can show multiple fragments at the same time to make up a whole screen. Within an activity, you can also swap out different fragments with each other. (You can also have invisible fragments as well, that do some work related to the activity, but we won’t cover those in this course.)

Fragments were introduced in Android when we started building for larger screen devices like tablets. Let me show you an example.

In the master/detail pattern, a list fragment is on the left side of the screen, while the detail fragment on the right swaps out depending on the list item selected.

So, I'm asking what's the utility of fragments apart from adapting to larger screens? Thanks !!

hasan_shaikh
  • 1,434
  • 1
  • 15
  • 38
Abdell00
  • 60
  • 12
  • here is one of a long list of existing questions that cover this https://stackoverflow.com/questions/13756999/why-should-i-use-fragment-in-android. Just google it next time – Tim Jul 31 '18 at 12:59
  • @TimCastelijns i googled it but i wanted a simple answer from someone advanced , as i'm beginner i can not understand everything that comes here in your link – Abdell00 Jul 31 '18 at 13:04
  • Usage of fragments explained here: https://stackoverflow.com/a/8597908/9744898 – Matthew-I Jul 31 '18 at 13:18

2 Answers2

1

It's mostly about reusable UI and self-contained code.

In a bigger project these can come in handy.

Also you can dynamically configure whats on the screen based on settings, maybe a/b testing, remote configuration etc. without lots of rewritten code. Team members can work independently on code parts if they are self-contained. Components can be tested in other activities, with different configurations.

So it really dependes on your use case.

breakline
  • 5,776
  • 8
  • 45
  • 84
1

You 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 (sort of like a "sub activity" that you can reuse in different activities).

Source : developer.android.com/guide/components/fragments

Faz
  • 322
  • 2
  • 14