To Fragment or not to Fragment - Nested Fragments against Activities. Why should I use more than one Activity?

AndroidAndroid ActivityAndroid Fragments

Android Problem Overview


There are a lot of discussions about whether you should use Activities or Fragments. For example:

Most of the discussions I found were released before Android 4.2.
With Android 4.2 Google invented nested Fragments.

Therefore I actually don't see any reason anymore to use more than one Activity.

In the early stage of Fragments they were supposed to be used within Apps for supporting Tablets and Smartphones in a comfortable way at the same time.

Thus for example you have a ListView which can open a detail View on click on an item. On a Smartphone we would replace the ListView and show the detailed View instead. Whereas a Tablet instead of replacing the List with the detail View can show both Views at the same time.


Now with nested Fragments there are a lot of other possibilities. In case you want to use a single Activity, you could store general information in the Activity and every Fragment would have access to it.

Besides this, Fragments who have nested Fragments, could also store information for their children Fragments.

With Fragments I can easily reuse the Views, I can show more than one Fragment at the same time and I can easly form a dialog out of a Fragment. This all would take me probably not more than just some copy & paste actions.

If I use Activities instead I seriously have to change a lot to get this done.


I recently implemented an Application where I easily could use two Fragment-ViewPager to get things really beautiful and dynamic (Some kind of: Today's Information - Yesterday's Information). In my opinion Fragments make our life a way easier :)


Questions:

  • Why should I use more than one Activity?

Could you provide any good example in which the usage of multiple Activities makes more sense instead of using Fragments?

  • Are there any good examples where you don't have any choice but to use Activities?

I think most of the bigger frameworks like Maps, YouTube and co already support Fragments. So we don't have to rely on Activities. Also is it quite easy to deal with NavigationBar, TabHosts, ViewPager, ActionBar in case you use Fragments.


From Udacity:

Why not always create one Activity with lots of Fragments?

  1. Increased Complexity

  2. Harder Intent handling

  3. Difficult to read, maintain and test

  4. Risk of tight coupling

  5. Security concerns

Android Solutions


Solution 1 - Android

First off, I will agree with you that it is possible to write a huge application using only one activity and nested fragments. That's the fun of software - you can achieve the same functionality using a variety of approaches. For me, the choice to use multiple activities comes down to my personal preferences for encapsulation, reusability, and testability.

If I have a widget that can be reused in other applications, I make it a Fragment. For example, my app features a "sync with server" button and I have created a custom Fragment that uses Progress Bars to visually show the synchronization process. It's easy to imagine another application being able to use this widget, so that's why I developed it as a Fragment.

If I am switching tasks within my app, such that the new task is conceptually independent of the previous task, then I use a new activity. For example, the first page of my app asks you to select a user. Once you've clicked on a user, I send you to the main menu for that user. This main menu for the user is displayed in a new activity.

Now let's imagine a large, complex app, and a team of developers assigned to develop that app. If the app can be divided into separate activities, it is conceptually very easy to divide up the tasks. Each activity is its own sandbox, so parallel development is simple and unit-testable. If there are any common needs, the team should still develop Fragments and reuse them, of course. I should add that code reuse does not happen often enough in software development, but if done right, there should be a lot of reusable Fragments.

Now suppose it is time to test the app. Your testing team can treat each activity as its own black box. This is easier to test than a single huge app that relies on a single activity and tons of nested fragments. This is especially important if a bug exists. If a bug exists that is not obvious, at least I know the scope of the bug is limited to a single activity out of many.

In summary, my guess is that you are developing your app as an individual, and therefore your development decisions do not affect anybody else. Your perspectives would likely change if you were developing an app with a larger team, and I hope my response makes a lot of sense in that light.

Solution 2 - Android

You are right. One could do alot with fragments alone. However, according to the Activity class, an activity is a single, focused thing that the user can do. I always tend to compartmentalize my application into activities at the top level and furthermore into fragments.

For example, here is a situation where multiple activities make sense. Our applications has a certain functionality that can be extended if the user logs in. However, the user might still use this limited functionality before signing in. Say, you can not comment on items except when you are logged in. In this case, our MainActivity might show the whole functionality. If the user wants to comment, we ask him to login by saving his request and starting an activity LoginActivity that handles login/registration and sets the right result when he is done. In our calling Fragment or Activity, we check if the result is LOGIN_SUCCESS or if the user is currently logged in and serve the pending request. What I am trying to say is that the action flow of Login should be a separate activity. Otherwise, handling would probably be messed up. In this way, any action that requires login can call startActivityForResult(LOGIN) and saves itself as a pendingAction. Then the after setting the result we could implement the handling in the super.onActivityResult. This is all theoretical of course, one could implement Login in a fragment with no trouble. However, the code will definitely turn up to be FUed.

Another situation where you need an Activity is when your activity provides an exported Service. For example, say you are a developer of a "File Uploader" and you receive intents to upload files. It is very convenient in this case to create an Activity that can consume requests of uploading a file.

Yet, with the current updates, Fragments' functionality span most of the features of android apps and therefore can be used alone with a single host activity to fulfill the requirements of an app.

However, your whole "a single activity host" with data there is rather a weak defense. Activities and closely Fragments has the whole lifecycle that include the save/restore instance via bundles. A single host activity can be long lasting and host multiple fragments that might be recycled multiple times in a single lifetime of their activity. It is therefore highly probable that an activity holding all these instances over time to exceed its budget of resources. It is the responsibility of the developer to keep data in a shared context when they are really shared among multiple objects. This is a drawback of this approach. It does not make sense in our example for the MainActivity to consume an extra byte of data because it hosted the Login fragments when it could have slept and freed its memory if needed.

In the end, a good programmer can manage to do the same task likewise.

Solution 3 - Android

You are totally right!

>Why should I use more than one Activity? > >>Could you provide any good example in which the usage of multiple Activities makes more sense instead of using Fragments? > > Are there any good examples where you don't have any choice but to use Activities? > >> I think most of the bigger frameworks like Maps, YouTube and co already support Fragments. So we don't have to rely on Activities. Also is it quite easy to deal with NavigationBar, TabHosts, ViewPager, ActionBar in case you use Fragments.

Always using Fragments is not good for inflating the data to UI, especially in case

  • ConnectionTimeOut (or low internet) since it took much time to initialize the data. So, sometimes using each activity to inflate few data is better than each fragment.

  • Or for using method onActivityResult for more flexible, using activity is also better than. Ex. You can call FileDialogChooser in your application.

That is some cases,

Thanks,

Solution 4 - Android

For your questions i can only say that sometimes, with a complex User Experience or complex app that must use a different hardware component you need to use activity instead of fragment.

For example, if you have to create an app with some form that have a step that take a photo and then use this photo for some work hard memory task ( face detection for example ) that use memory you need to separate this task from the main task activity and personalise the permission on manifest to use more memory only on this activity.

Another example is if you want to use weak memory activity ( in manifest you can set properties that the clear the activity stack and force the Garbage Collector to clean memory when the activity finish )

The most probably situation that requires to use more activity and fragment is the User Experience of the app. If you need a right drawer custom that contains a menu and every fragment of the menu contains a listView and every list view must go in a detail. You can do a lot of trick to hide the right drawer and create animation to slide from fragment to the detail, but the best and simplest way is to make a new activity for the detail and manage it with a separate logic/lifecycle from the main activity with the drawer. I've done this choose in two of my apps:

iMeal - https://play.google.com/store/apps/details?id=it.fullsix.chiccopappe

GGRugby - https://play.google.com/store/apps/details?id=it.f6.template

In this app the Drawer is in the Main Fragment Activity, every menu change the content fragment. The list view in the content fragment change activity context with intent and go in another activity.

Finally there are some situation that i think you should work with more that one only fragmentActivity...but this a my pattern of work, probably you can find a trick/way or scope to do something with only an Activity and fragment.

Solution 5 - Android

I know I'm too late for this but Square's got opinion about fragments. Here is the gist of the article:

> Fragments: lessons learned > > Despite their drawbacks, fragments taught us invaluable lessons which we can now reapply when writing apps: > > - Single Activity Interface: there is no need to use one activity for each screen. We can split our app into decoupled widgets and assemble them as we please. This makes animating and lifecycle easy. We can split our widgets into view code and controller code. > - The backstack isn’t an activity specific notion; you can implement a backstack within an activity. > - There is no need for new APIs; everything we needed was there from the very beginning: activities, views, and layout inflaters.

You don't have to use more than one activity and you don't have to use fragments. You can just use custom views with their presenters.

You can read it here: https://corner.squareup.com/2014/10/advocating-against-android-fragments.html.

You can also find Square's mortar library here: https://github.com/square/mortar

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFrame91View Question on Stackoverflow
Solution 1 - AndroidStevenView Answer on Stackoverflow
Solution 2 - AndroidSherif elKhatibView Answer on Stackoverflow
Solution 3 - AndroidHuy TowerView Answer on Stackoverflow
Solution 4 - Androidphemt.latdView Answer on Stackoverflow
Solution 5 - AndroidPenguinBluesView Answer on Stackoverflow