7

I'm planing a new app with a navigation drawer. It is better to use one activity which manage many fragments or to use many activities with many fragments? Are there any serious advantages or disadvantages?

I read many articles about this but there are from 2012 and older.

(I'm just planing to read and insert some datas into/from the database..)

Tobias
  • 4,921
  • 4
  • 31
  • 40
  • Possible duplicate of [One Activity and all other Fragments](https://stackoverflow.com/questions/12154505/one-activity-and-all-other-fragments) – Pouya Samie Aug 27 '17 at 19:45

1 Answers1

7

From my experience I would recommend rather having multiple activities with many fragments. If you use a single activity you are going to find it harder and harder to manage fragments with the activity lifecycle.

For example if the activity is destroyed (e.g. if phone is low on memory and user receives a phone call or you call an intent to open the camera), when the intent is recreated you will need to handle recreating the fragments and their states.

With a single activity this can quickly become a nightmare to manage if not done carefully. With multiple activities it's easier to manage and helps seperate portions of the app - making it easier to debug as well.

An example of how something simple can become complex with a single activity would be something such as the back button.

If you need to handle it different for different fragments that means your activity is going to need to cater for which fragment is currently visible, as the activity overrides the backbutton, not the fragment. This also would likely mean you need to add interfaces to notify the fragments of a back button press.

Stating all of this however, there are some apps that benefit from a single activity. For example if you have a viewpager for swiping fragments (e.g. pages of a book) or fragments that do little interaction, then a single activity can be beneficial.

hyarion
  • 2,251
  • 2
  • 17
  • 28
  • 1
    I am trying to use one activity and I do keep getting stuck. It is making me wonder if I shouldn't be using a single activity architecture. What I hate, though, is that fragments load in so much faster than launching a new activity. I feel that if a single activity architecture is used then the app will feel a lot more responsive. Quite a conundrum. – theblang May 15 '14 at 20:26