Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

  • To-Fragment-or-not-to-Fragment
  • One Activity and all other Fragments
  • How many Activities vs. Fragments
  • Use Activity or Fragment in Android Application

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
like image 423
Frame91 Avatar asked Nov 12 '13 11:11

Frame91


People also ask

Why fragments are better than activities?

Like activities, they have a specific lifecycle, unlike activities, they are not top-level application components. Advantages of fragments include code reuse and modularity (e.g., using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets).

Is it better to use fragments or activities?

After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity. While Using fragments in the project, the project structure will be good and we can handle it easily.

What is the purpose of using fragments in an activity?

Fragments introduce modularity and reusability into your activity's UI by allowing you to divide the UI into discrete chunks. Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer.

What is the recommended way to pass arguments to a fragment?

To pass info to a fragment , you setArguments when you create it, and you can retrieve this argument later on the method onCreate or onCreateView of your fragment. Then inside the fragment on the method onCreate or onCreateView you can retrieve the arguments like this: Bundle args = getArguments(); int index = args.


3 Answers

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.

like image 136
Steven Avatar answered Oct 23 '22 02:10

Steven


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.

like image 2
Sherif elKhatib Avatar answered Oct 23 '22 01:10

Sherif elKhatib


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,

like image 2
Huy Tower Avatar answered Oct 23 '22 02:10

Huy Tower