0

I read some question here about Fragments vs Activities, but I do not really know when I should use which. I want to create an app where I have a NavigationDrawer and in the drawer I can choose for an example "Settings". Should I now use a fregment or an activities. At the moment my plan is, the NavigationDrawer is a singleton and I have a BaseActivity which have the NavigationDrawer and all new activities inherit from BaseActivity.

Greetz

Alex
  • 4,457
  • 2
  • 20
  • 59
R3Tech
  • 743
  • 2
  • 9
  • 22
  • Most content should be in a fragment. Activities are always fullscreen and have an ugly lifecycle. http://stackoverflow.com/questions/20306091/dilemma-when-to-use-fragments-vs-activities – zapl Mar 10 '16 at 13:20
  • 1
    It really is up to you - I prefer `Fragment`s, but sometimes it's worth creating a new empty `Activity` to hold data, whilst the `Fragment` provides the UI. It looks as though this question is going to provide [opinion-based answers](http://stackoverflow.com/help/dont-ask) though, so I'm going to flag it as off-topic for SO. – PPartisan Mar 10 '16 at 13:23
  • with navigation drawer menu items you should use fragments. But it is up to you what suites you best – Vivek Mishra Mar 10 '16 at 13:27
  • @zapl this post i read. I read tha fregments doesnt handle the "return button". So the App will close if I press the return button. With activities not. – R3Tech Mar 10 '16 at 13:35

2 Answers2

2

You can think that fragments are like modules of a screen, that way you can place in different ways depending on the screen size (for instance tablet or phone). With the Navigation Drawer, you can use fragments or activities. But I highly reccomend to use fragments and one Activity. That way, the user will be able to access to NavigationView from every screen listed in that menu. You only have to implement the NavigationView in the main Activity.

antonicg
  • 934
  • 10
  • 24
1

Fragmentcan be defined as a modular section of an Activity. Activities hold Fragments in their layout XML but Fragments do not hold Activities.

The main purpose of Fragment is that it allows flexibility over the UI of the Activity. Suppose, when the app is opened on a phone you want that it to show two separate Activity Intents but on a tablet you want both the intent's XML to viewed side by side on one screen. So do you really make different XML files for different phone sizes? No, life is hard as it is. You create parts of the Activities, sub activities or Fragments and show it programmatically depending on the circumstances.

Shababb Karim
  • 3,614
  • 1
  • 22
  • 35