I know that Activities
are designed to represent a single screen of my application, while Fragments
are designed to be reusable UI layouts with log
It depends what you want to build really. For example the navigation drawer
uses fragments. Tabs use fragments
as well. Another good implementation,is where you have a listview
. When you rotate the phone and click a row the activity is shown in the remaining half of the screen. Personally,I use fragments
and fragment dialogs
,as it is more professional. Plus they are handled easier in rotation.
In my opinion it's not really relevant. The key factor to consider is
The main use of fragments is to build multipane activities, which makes it perfect for Tablet/Phone responsive apps.
Don't forget that an activity is application's block/component which can be shared and started through Intent! So each activity in your application should solve only one kind of task. If you have only one task in your application then I think you need only one activity and many fragments if needed. Of course you can reuse fragments in future activities which solve another tasks. This approach will be clear and logical separation of tasks. And you no need to maintain one activity with different intent filter parameters for different sets of fragments. You define tasks at the design stage of the development process based on requirements.
Thing I did: Using less fragment when possible. Unfortunately, it's possible in almost case. So, I end up with a lot of fragments and a little of activities. Some drawbacks I've realized:
ActionBar
& Menu: When 2 fragment has different title, menu, thatbackstack
there is no way to restore the old title. You may need an Toolbar in every fragment for this case, but let believe me, that will spend you more time. startForResult
, activity has but fragment hasn't.My solution for this is using an Activity to wrap a fragment inside. So we have separate action bar, menu, startActivityForResult
, animation,...
Since Jetpack, Single-Activity app is the preferred architecture. Usefull especially with the Navigation Architecture Component.
source
use one activity per application to provide base for fragment
use fragment
for screen ,
fragments
are lite weight as compared to activites
fragments are reusable
fragments are better suited for app which support both phone & tablet