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?
- Increased Complexity
- Harder Intent handling
- Difficult to read, maintain and test
- Risk of tight coupling
- Security concerns