One Activity and All Other Fragments

Single Activity with multiple fragments

Welcome to Android. Buckle up, it's going to be a bumpy ride.

First, you should spend some quality time with the Android Developer Guides.

Developer Guides | Android Developers

The insight for your particular challenge is that layouts are resources, and and you can have a layout resource that changes based on the configuration.

So for example, you could have a resource like layout_main with one XML file for small devices and a different XML file for large devices.

App resources overview | Android Developers

Create alternate layouts | Support different screen sizes | Android Developers

You can arrange fragments on a layout using the Android Studio layout designer.

One activity - many fragments OR many activities - many fragments?

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.

Is there any point of an Activity with one fragment?

Out of my personal opinion, I would say yes.

For the following reasons:

  • Assuming you are familiar with Fragments, creating a Fragment is hardly any extra work plus has the following benefits
  • Fragments can easily be reused somewhere else (possibly another Activity that has more Fragments, furthermore, Fragments do not necessarily need to use up the full screen).
  • Activity transitions are more expensive, Fragment transitions are more sophisticated.
  • The Fragment animation framework is better (in terms of usability and performance).
  • I always like to keep the number of Activities to a minimum which keeps the AndroidManifest.xml short and clean.
  • UI separated into Fragments leads to cleaner code structure and easier code maintenance.

According to google coding guidelines, it is best practice to create as few Activities as possible, and create multiple Fragments instead that are switched inside an Activity.

Why using two or more fragments in the same Activity?

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

Navigation using one activity and one toolbar with multiple fragments

I would recommend you to implement an interface to manage backstack.
Here is a good blog post which would help you understand this process



Related Topics



Leave a reply



Submit