Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Activity and all other Fragments [closed]

People also ask

Can one activity have multiple fragments?

We can use one to multiple fragments in a single activity. Based on the app requirements, we might use fragments in our app or we might not use and cover the most of the screens using Activity classes. Fragments are now widely used in Android apps after adopting Navigation components.

How many fragments can there be in one activity?

Two Fragments should never communicate directly. Show activity on this post. 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.


It depends on the app you are creating. I've created several apps using both approaches and can't say one way is always better than the other. The latest app I created I used the single Activity approach and a Facebook style navigation. When selecting items from the navigation list I update a single Fragment container to display that section.

That said, having a single Activity also introduces a lot of complexities. Let's say you have an edit form, and for some of the items the user needs to select, or create, requires them to go to a new screen. With activities we'd just call the new screen with startActivityForResult but with Fragments there is no such thing so you end up storing the value on the Activity and having the main edit fragment check the Activity to see if data has been selected and should be displayed to the user.

What Aravind says about being stuck to a single Activity type is also true but not really that limiting. Your activity would be a FragmentActivity and as long as you don't need a MapView then there are no real limitations. If you do want to display maps though, it can be done, but you'll need to either modify the Android Compatibility Library to have FragmentActivity extend MapActivity or use the the publicly available android-support-v4-googlemaps.

Ultimately most the devs I know that went the one Activity route have gone back to multiple Activities to simplify their code. UI wise, on a tablet, you are some times stuck using a single Activity just to achieve what ever crazy interaction your designers come up with :)

-- EDIT --

Google has finally released MapFragment to the compatibility library so you no longer have to use the android-support-v4-googlemaps hack. Read about the update here: Google Maps Android API v2

-- EDIT 2 --

I just read this great post about the modern (2017) state of fragments and remembered this old answer. Thought I would share: Fragments: The Solution to All of Android's Problems


I'm about to finish a project(5 months in development), that has 1 activity, and 17 fragments, all full screen. This is my second fragment based project(previous was 4 months).

Pros

  • The main activity is 700 lines of code, just nicely managing the order of the fragments navigation.
  • Each fragment is nicely separated into it's own class, and is relatively small (~couple hundred lines of ui stuff).
  • Management can say, "hey, how about we switch the order of those screens", and I can do it very easily, as those fragments don't depend on each other, they all communicate through the activity. I don't have to dig through individual activities, to find where they call each other.
  • my app is very graphics heavy, and would never work as 1 screen 1 activity. All those sub activities in the memory, would make the app run out of memory all the time, so I would have to finish() all non visible activities, and make the same control logic for navigation, as I would do with fragments. Might as well do it with fragments just because of this.
  • if we ever do a tablet app, we will have an easier time re-factoring stuff, because everything is nicely separated already.

Cons

  • you have to learn, how to use fragments

First, whatever you do, make sure you have a modular design using model, view, presenter that is not highly dependent on an Activity or a Fragment.

What do Activities and Fragments really provide?

  1. Life cycle events and backstack
  2. Context and resources

Therefore, use them for that, ONLY. They have enough responsibility, don't over complicate them. I would argue that even intantiating a TextView in an Activity or Fragment is bad practice. There is a reason methods like public View findViewById (int id) are PUBLIC.

Now the question gets simpler: Do I need multiple, independent life cycle events and backstacks? If you think yeah maybe, use fragments. If you think never ever, don't use fragments.

In the end, you could make your own backstack and life cycles. But, why recreate the wheel?

EDIT: Why down vote this? Single purpose classes people! Each activity or fragment should be able to instantiate a presenter that instantiates a view. The presenter and view are a module that can be interchanged. Why should an activity or fragment have the responsibility of a presenter??


Pros

You could control your fragments from a single activity, beacause all fragments are independent of each other. The fragments have a lifecycle (onPause, onCreate, onStart...) of their own. By having a lifecycle, fragments can respond independently to events, save their state through onSaveInstanceState, and be brought back (i.e. such as when resuming after an incoming call or when the user clicks the back button).

Cons

  1. Create complexity in your code of activity.
  2. You have to manage the order of the fragments.

Never the less, it is quite a good idea, as if you need to create an app, where you want to show several views. By this idea, you'll be able to view several fragments in a single view..


It depends on the design layout of your app. Suppose if your using Tabs in ActionBar in the design layout then in the Single Activity of the app one can have fragments being changed on Tab click. So now you have an Activity and say suppose three Tabs in the ActionBar and the view for the tabs being provided by the Fragments, which makes it easy to manage plus is feasible also. So, it all depends on the design scheme of your app and how you take the decision to build for it.