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..)
Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Activity is not dependent on fragment. Fragment is dependent on activity. It can't exist independently.
There is no official guideline regarding the limit of fragments that can be added to an activity. You can have as many as you need and looks logical for your app.
Yes you can, but you have to add more logic to your fragments and add some interfaces for each activity.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With