Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

android - 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:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...