Dilemma: when to use Fragments vs Activities:









up vote
695
down vote

favorite
275












I know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with logic embedded inside of them.



Until not long ago, I developed an application as it said that they should be developed.
I created an Activity to represent a screen of my application and used Fragments for ViewPager or Google Maps. I rarely created a ListFragment or other UI that can be reused several times.



Recently I stumbled on a project that contains only 2 Activities one is a SettingsActivity and other one is the MainActivity. The layout of the MainActivity is populated with many hidden full screen UI fragments and only one is shown. In the Acitivty logic there are many FragmentTransitions between the different screens of the application.



What I liked about this approach is that because the application uses an ActionBar, it stays intact and does not move with the screen switching animation, which is what happens with Activity switching. This give a more fluent feel to those screen transitions.



So I guess what I'm asking is to share your current development manner regarding this topic, I know it might look like an opinion based question at first look but I look at it as an Android design and architecture question... Not really an opinion based one.



UPDATE (01.05.2014): Following this presentation by Eric Burke from Square, (which I have to say is a great presentation with a lot of useful tools for android developers. And I am not related in any way to Square)



http://www.infoq.com/presentations/Android-Design/



From my personal experience over the past few months, I found that the best way to construct my applications is to create groups of fragments that come to represent a flow in the application and present all those fragments in one Activity. So basically you will have the same number of Activities in your application as the number of flows.
That way the action bar stays intact on all the flow's screens, but is being recreated on changing a flow which makes a lot of sense. As Eric Burke states and as I have come to realize as well, the philosophy of using as few Activities as possible is not applicable for all situations because it creates a mess in what he calls the "God" activity.










share|improve this question



















  • 295




    For all who voted to close this question as opinion-based, it's not really true... It's a Android design and architecture question... Not really an opinion based one.
    – Emil Adz
    Dec 1 '13 at 12:08






  • 9




    Agreed. True; there are many right ways to answer this question, but, as can be seen in many ridiculous answers to questions, just because something will "work" does not make it a desirable solution. This is not a matter of opinion. We are looking for the best right answer, or at least one that is roughly equivalent to the other "best" solutions.
    – brainmurphy1
    Jun 11 '14 at 22:35







  • 2




    Check out my post at SO - stackoverflow.com/questions/24647078/…
    – My God
    Sep 1 '14 at 6:58






  • 3




    Now, the only thing left for me to do, is to determine what a flow should be like and not be like.
    – Stephane
    Sep 27 '15 at 16:08






  • 2




    @StephaneEybert, I think this depends on the design of the application. usually all those configurations would be part of the settings screen flow. So in this case all those options come under the same flow (activity) with different fragments that are swapped in order to progress the flow. In case those options are part of the navigation drawer. maybe each of those options should be a separate flow.
    – Emil Adz
    Sep 29 '15 at 10:32














up vote
695
down vote

favorite
275












I know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with logic embedded inside of them.



Until not long ago, I developed an application as it said that they should be developed.
I created an Activity to represent a screen of my application and used Fragments for ViewPager or Google Maps. I rarely created a ListFragment or other UI that can be reused several times.



Recently I stumbled on a project that contains only 2 Activities one is a SettingsActivity and other one is the MainActivity. The layout of the MainActivity is populated with many hidden full screen UI fragments and only one is shown. In the Acitivty logic there are many FragmentTransitions between the different screens of the application.



What I liked about this approach is that because the application uses an ActionBar, it stays intact and does not move with the screen switching animation, which is what happens with Activity switching. This give a more fluent feel to those screen transitions.



So I guess what I'm asking is to share your current development manner regarding this topic, I know it might look like an opinion based question at first look but I look at it as an Android design and architecture question... Not really an opinion based one.



UPDATE (01.05.2014): Following this presentation by Eric Burke from Square, (which I have to say is a great presentation with a lot of useful tools for android developers. And I am not related in any way to Square)



http://www.infoq.com/presentations/Android-Design/



From my personal experience over the past few months, I found that the best way to construct my applications is to create groups of fragments that come to represent a flow in the application and present all those fragments in one Activity. So basically you will have the same number of Activities in your application as the number of flows.
That way the action bar stays intact on all the flow's screens, but is being recreated on changing a flow which makes a lot of sense. As Eric Burke states and as I have come to realize as well, the philosophy of using as few Activities as possible is not applicable for all situations because it creates a mess in what he calls the "God" activity.










share|improve this question



















  • 295




    For all who voted to close this question as opinion-based, it's not really true... It's a Android design and architecture question... Not really an opinion based one.
    – Emil Adz
    Dec 1 '13 at 12:08






  • 9




    Agreed. True; there are many right ways to answer this question, but, as can be seen in many ridiculous answers to questions, just because something will "work" does not make it a desirable solution. This is not a matter of opinion. We are looking for the best right answer, or at least one that is roughly equivalent to the other "best" solutions.
    – brainmurphy1
    Jun 11 '14 at 22:35







  • 2




    Check out my post at SO - stackoverflow.com/questions/24647078/…
    – My God
    Sep 1 '14 at 6:58






  • 3




    Now, the only thing left for me to do, is to determine what a flow should be like and not be like.
    – Stephane
    Sep 27 '15 at 16:08






  • 2




    @StephaneEybert, I think this depends on the design of the application. usually all those configurations would be part of the settings screen flow. So in this case all those options come under the same flow (activity) with different fragments that are swapped in order to progress the flow. In case those options are part of the navigation drawer. maybe each of those options should be a separate flow.
    – Emil Adz
    Sep 29 '15 at 10:32












up vote
695
down vote

favorite
275









up vote
695
down vote

favorite
275






275





I know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with logic embedded inside of them.



Until not long ago, I developed an application as it said that they should be developed.
I created an Activity to represent a screen of my application and used Fragments for ViewPager or Google Maps. I rarely created a ListFragment or other UI that can be reused several times.



Recently I stumbled on a project that contains only 2 Activities one is a SettingsActivity and other one is the MainActivity. The layout of the MainActivity is populated with many hidden full screen UI fragments and only one is shown. In the Acitivty logic there are many FragmentTransitions between the different screens of the application.



What I liked about this approach is that because the application uses an ActionBar, it stays intact and does not move with the screen switching animation, which is what happens with Activity switching. This give a more fluent feel to those screen transitions.



So I guess what I'm asking is to share your current development manner regarding this topic, I know it might look like an opinion based question at first look but I look at it as an Android design and architecture question... Not really an opinion based one.



UPDATE (01.05.2014): Following this presentation by Eric Burke from Square, (which I have to say is a great presentation with a lot of useful tools for android developers. And I am not related in any way to Square)



http://www.infoq.com/presentations/Android-Design/



From my personal experience over the past few months, I found that the best way to construct my applications is to create groups of fragments that come to represent a flow in the application and present all those fragments in one Activity. So basically you will have the same number of Activities in your application as the number of flows.
That way the action bar stays intact on all the flow's screens, but is being recreated on changing a flow which makes a lot of sense. As Eric Burke states and as I have come to realize as well, the philosophy of using as few Activities as possible is not applicable for all situations because it creates a mess in what he calls the "God" activity.










share|improve this question















I know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with logic embedded inside of them.



Until not long ago, I developed an application as it said that they should be developed.
I created an Activity to represent a screen of my application and used Fragments for ViewPager or Google Maps. I rarely created a ListFragment or other UI that can be reused several times.



Recently I stumbled on a project that contains only 2 Activities one is a SettingsActivity and other one is the MainActivity. The layout of the MainActivity is populated with many hidden full screen UI fragments and only one is shown. In the Acitivty logic there are many FragmentTransitions between the different screens of the application.



What I liked about this approach is that because the application uses an ActionBar, it stays intact and does not move with the screen switching animation, which is what happens with Activity switching. This give a more fluent feel to those screen transitions.



So I guess what I'm asking is to share your current development manner regarding this topic, I know it might look like an opinion based question at first look but I look at it as an Android design and architecture question... Not really an opinion based one.



UPDATE (01.05.2014): Following this presentation by Eric Burke from Square, (which I have to say is a great presentation with a lot of useful tools for android developers. And I am not related in any way to Square)



http://www.infoq.com/presentations/Android-Design/



From my personal experience over the past few months, I found that the best way to construct my applications is to create groups of fragments that come to represent a flow in the application and present all those fragments in one Activity. So basically you will have the same number of Activities in your application as the number of flows.
That way the action bar stays intact on all the flow's screens, but is being recreated on changing a flow which makes a lot of sense. As Eric Burke states and as I have come to realize as well, the philosophy of using as few Activities as possible is not applicable for all situations because it creates a mess in what he calls the "God" activity.







android android-fragments android-activity architecture






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 18 '17 at 1:34









Henry

12.1k54275




12.1k54275










asked Nov 30 '13 at 21:53









Emil Adz

27.2k32109150




27.2k32109150







  • 295




    For all who voted to close this question as opinion-based, it's not really true... It's a Android design and architecture question... Not really an opinion based one.
    – Emil Adz
    Dec 1 '13 at 12:08






  • 9




    Agreed. True; there are many right ways to answer this question, but, as can be seen in many ridiculous answers to questions, just because something will "work" does not make it a desirable solution. This is not a matter of opinion. We are looking for the best right answer, or at least one that is roughly equivalent to the other "best" solutions.
    – brainmurphy1
    Jun 11 '14 at 22:35







  • 2




    Check out my post at SO - stackoverflow.com/questions/24647078/…
    – My God
    Sep 1 '14 at 6:58






  • 3




    Now, the only thing left for me to do, is to determine what a flow should be like and not be like.
    – Stephane
    Sep 27 '15 at 16:08






  • 2




    @StephaneEybert, I think this depends on the design of the application. usually all those configurations would be part of the settings screen flow. So in this case all those options come under the same flow (activity) with different fragments that are swapped in order to progress the flow. In case those options are part of the navigation drawer. maybe each of those options should be a separate flow.
    – Emil Adz
    Sep 29 '15 at 10:32












  • 295




    For all who voted to close this question as opinion-based, it's not really true... It's a Android design and architecture question... Not really an opinion based one.
    – Emil Adz
    Dec 1 '13 at 12:08






  • 9




    Agreed. True; there are many right ways to answer this question, but, as can be seen in many ridiculous answers to questions, just because something will "work" does not make it a desirable solution. This is not a matter of opinion. We are looking for the best right answer, or at least one that is roughly equivalent to the other "best" solutions.
    – brainmurphy1
    Jun 11 '14 at 22:35







  • 2




    Check out my post at SO - stackoverflow.com/questions/24647078/…
    – My God
    Sep 1 '14 at 6:58






  • 3




    Now, the only thing left for me to do, is to determine what a flow should be like and not be like.
    – Stephane
    Sep 27 '15 at 16:08






  • 2




    @StephaneEybert, I think this depends on the design of the application. usually all those configurations would be part of the settings screen flow. So in this case all those options come under the same flow (activity) with different fragments that are swapped in order to progress the flow. In case those options are part of the navigation drawer. maybe each of those options should be a separate flow.
    – Emil Adz
    Sep 29 '15 at 10:32







295




295




For all who voted to close this question as opinion-based, it's not really true... It's a Android design and architecture question... Not really an opinion based one.
– Emil Adz
Dec 1 '13 at 12:08




For all who voted to close this question as opinion-based, it's not really true... It's a Android design and architecture question... Not really an opinion based one.
– Emil Adz
Dec 1 '13 at 12:08




9




9




Agreed. True; there are many right ways to answer this question, but, as can be seen in many ridiculous answers to questions, just because something will "work" does not make it a desirable solution. This is not a matter of opinion. We are looking for the best right answer, or at least one that is roughly equivalent to the other "best" solutions.
– brainmurphy1
Jun 11 '14 at 22:35





Agreed. True; there are many right ways to answer this question, but, as can be seen in many ridiculous answers to questions, just because something will "work" does not make it a desirable solution. This is not a matter of opinion. We are looking for the best right answer, or at least one that is roughly equivalent to the other "best" solutions.
– brainmurphy1
Jun 11 '14 at 22:35





2




2




Check out my post at SO - stackoverflow.com/questions/24647078/…
– My God
Sep 1 '14 at 6:58




Check out my post at SO - stackoverflow.com/questions/24647078/…
– My God
Sep 1 '14 at 6:58




3




3




Now, the only thing left for me to do, is to determine what a flow should be like and not be like.
– Stephane
Sep 27 '15 at 16:08




Now, the only thing left for me to do, is to determine what a flow should be like and not be like.
– Stephane
Sep 27 '15 at 16:08




2




2




@StephaneEybert, I think this depends on the design of the application. usually all those configurations would be part of the settings screen flow. So in this case all those options come under the same flow (activity) with different fragments that are swapped in order to progress the flow. In case those options are part of the navigation drawer. maybe each of those options should be a separate flow.
– Emil Adz
Sep 29 '15 at 10:32




@StephaneEybert, I think this depends on the design of the application. usually all those configurations would be part of the settings screen flow. So in this case all those options come under the same flow (activity) with different fragments that are swapped in order to progress the flow. In case those options are part of the navigation drawer. maybe each of those options should be a separate flow.
– Emil Adz
Sep 29 '15 at 10:32












13 Answers
13






active

oldest

votes

















up vote
236
down vote













Experts will tell you: "When I see the UI, I will know whether to use an Activity or a Fragment". In the beginning this will not have any sense, but in time, you will actually be able to tell if you need Fragment or not.



There is a good practice I found very helpful for me. It occurred to me while I was trying to explain something to my daughter.



Namely, imagine a box which represents a screen. Can you load another screen in this box? If you use a new box, will you have to copy multiple items from the 1st box? If the answer is Yes, then you should use Fragments, because the root Activity can hold all duplicated elements to save you time in creating them, and you can simply replace parts of the box.



But don't forget that you always need a box container (Activity) or your parts will be dispersed. So one box with parts inside.



Take care not to misuse the box. Android UX experts advise (you can find them on YouTube) when we should explicitly load another Activity, instead to use a Fragment (like when we deal with the Navigation Drawer which has categories). Once you feel comfortable with Fragments, you can watch all their videos. Even more they are mandatory material.



Can you right now look at your UI and figure out if you need an Activity or a Fragment? Did you get a new perspective? I think you did.






share|improve this answer


















  • 4




    do you have a link to the youtube feed you mentioned? I search for "Android UX experts" and "Android UX" but am not entirely sure which videos you're talking about.
    – me--
    Jun 2 '16 at 8:15






  • 2




    Not any more, watched it over a year ago. Search for Android developer official talking about UX
    – sandalone
    Jun 2 '16 at 8:17






  • 1




    One example of consideration: activity has parentActivity so we can synthesize backstack while entering from notification, but I don't think there's such parentFragment.
    – fikr4n
    Feb 24 '17 at 22:51










  • @BornToCode there is getParentFragment: developer.android.com/reference/android/support/v4/app/…
    – ToolmakerSteve
    May 5 at 21:13











  • @ToolmakerSteve yes it's getParentFragment, but it's not what I meant dude, see developer.android.com/guide/topics/manifest/…
    – fikr4n
    May 14 at 7:42

















up vote
108
down vote













My philosophy is this:



Create an Activity only if it's absolutely absolutely required. With the backstack made available for committing bunch of fragment transactions, I try to create as minimum of Activities in my app as possible. Also, communicating between various fragments is much easier rather than sending data back and forth between activities.



Activity transitions are expensive, right? At least I believe so - since the old activity as to be destroyed/paused/stopped, pushed onto the stack and then the new activity has to be created/started/resumed.



It's just my philosophy since fragments were introduced.






share|improve this answer






















  • true, but as you've written, sometimes it is needed to use activities. one example is a camera screen, where it's better to use it in landscape mode. another example is the configuration screen that is shown when you put a customized appWidget (on the "desktop"- the launcher app).
    – android developer
    Nov 30 '13 at 23:22










  • Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
    – Emil Adz
    Dec 1 '13 at 12:02










  • @EmilAdz Yeah. I think so. Limit your app to use as minimum activities as possible - architect it in such a way if possible.
    – VJ Vélan Solutions
    Dec 1 '13 at 12:05










  • Then how do you solve the issue of fragments needing to pass eachother "state"? All of the state across all of your fragments need to live in the one activity, otherwise you're forced to use a singleton.
    – Mr_E
    Sep 27 '16 at 20:40






  • 13




    I'm not convinced that communicating between various fragments is much easier rather than sending data back and forth between activities.
    – Denny
    Mar 18 '17 at 23:11

















up vote
54
down vote













Well, according to Google's lectures (maybe here, I don't remember) , you should consider using Fragments whenever it's possible, as it makes your code easier to maintain and control.



However, I think that on some cases it can get too complex, as the activity that hosts the fragments need to navigate/communicate between them.



I think you should decide by yourself what's best for you. It's usually not that hard to convert an activity to a fragment and vice versa.



I've created a post about this dillema here, if you wish to read some further.






share|improve this answer


















  • 4




    Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
    – Emil Adz
    Dec 1 '13 at 12:02










  • It depends on the project, but if it gets too complicated to you, you can separate to multiple activities too. Don't be afraid to use any of the methods. You could also use them both. Maybe sometimes it would be too hard for you to use fragments instead of activities. I think you should try to use fragments, but don't force it to be everywhere if it gets too much in your way...
    – android developer
    Dec 1 '13 at 12:49











  • what if I want to keep this affect of the ActionBar staying intact and all the content is being switched? Is it possible to achieve this with Activities?
    – Emil Adz
    Dec 1 '13 at 13:08










  • What the Fragment? youtu.be/k3IT-IJ0J98?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC
    – Emer
    Oct 1 '16 at 0:04

















up vote
22
down vote













Why I prefer Fragment over Activity in ALL CASES.



  • Activity is expensive. In Fragment, views and property states are separated - whenever a fragment is in backstack, its views will be destroyed. So you can stack much more Fragments than Activity.


  • Backstack manipulation. With FragmentManager, it's easy to clear all the Fragments, insert more than on Fragments and etcs. But for Activity, it will be a nightmare to manipulate those stuff.


  • A much predictable lifecycle. As long as the host Activity is not recycled. the Fragments in the backstack will not be recycled. So it's possible to use FragmentManager::getFragments() to find specific Fragment (not encouraged).






share|improve this answer






















  • HI, I read your review about the advantages of Frag over Act, Do you have any project to show the same in your Github Repo?
    – Ümañg ßürmån
    Nov 15 at 5:53

















up vote
12
down vote













In my opinion it's not really relevant. The key factor to consider is



  1. how often are you gonna reuse parts of the UI (menus for example),

  2. is the app also for tablets?

The main use of fragments is to build multipane activities, which makes it perfect for Tablet/Phone responsive apps.






share|improve this answer






















  • I would say the main use of fragments is to make custom views without thinking of them as custom views. that's what happens anyways. Fragments we're originally shown from Google as a handy way to make tablet responsive apps, so you could stick them in different activities if you wanted. a way to attach code to a view, more or less, and have them stickable where you want(without making custom views).
    – Lassi Kinnunen
    Feb 28 at 15:36

















up vote
8
down vote













There's more to this than you realize, you have to remember than an activity that is launched does not implicitly destroy the calling activity. Sure, you can set it up such that your user clicks a button to go to a page, you start that page's activity and destroy the current one. This causes a lot of overhead. The best guide I can give you is:



** Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows).



A great example of when it makes sense to have multiple activities is Google Drive. The main activity provides a file explorer. When a file is opened, a new activity is launched to view that file. You can press the recent apps button which will allow you to go back to the browser without closing the opened document, then perhaps even open another document in parallel to the first.






share|improve this answer




















  • Re "Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows)." I don't think so. That situation is well solved using fragments attach/detach methods.
    – ToolmakerSteve
    Sep 20 '16 at 16:17

















up vote
7
down vote













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.






share|improve this answer





























    up vote
    7
    down vote













    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, that

      will hard to handle. Ex: when adding new fragment, you can change action bar title, but when pop it from backstack 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.

    • When we need startForResult, activity has but fragment hasn't.

    • Don't have transition animation by default

    My solution for this is using an Activity to wrap a fragment inside. So we have separate action bar, menu, startActivityForResult, animation,...






    share|improve this answer


















    • 1




      Very useful points, thanks. Can you clarify "an Activity to wrap a fragment"? Did you make a separate Activity for each fragment? If so, do you need Fragment at all?
      – ToolmakerSteve
      Sep 20 '16 at 15:24







    • 1




      there is a way to restore title and stuff. use getSupportFragmentManager().addOnBackStackChangedListener to add a listener. get current fragment in that listener and then set title and stuff.
      – babay
      Sep 9 '17 at 2:18

















    up vote
    3
    down vote













    The one big advantage of a fragment over activity is that , the code which is used for fragment can be used for different activities.so, it provides re-usability of code in application development.






    share|improve this answer


















    • 3




      How? COuld you provide some example please?
      – sofs1
      Dec 24 '16 at 11:03

















    up vote
    2
    down vote













    You are free to use one of those.

    Basically, you have to evaluate which is the best one to your app. Think about how you will manage the business flow and how to store/manage data preferences.



    Think about, how Fragments store garbage data. When you implement the fragment, you have a activity root to fill with fragment(s). So, if your trying to implement a lot of activities with too much fragments, you have to consider performance on your app, coz you're manipulating (coarsely speaks) two context lifecycle, remember the complexity.



    Remember: should I use fragments? Why shouldn't I?



    regards.






    share|improve this answer



























      up vote
      1
      down vote













      I use Fragments for better user experience. For example if you have a Button and you want to run let's say a webservice when you click it, I attach a Fragment to the parent Activity.



      if (id == R.id.forecast) 

      ForecastFragment forecastFragment = new ForecastFragment();
      FragmentManager fm = getSupportFragmentManager();
      FragmentTransaction ft = fm.beginTransaction();
      ft.replace(R.id.main_content, forecastFragment);
      ft.addToBackStack("backstack");
      forecastFragment.setArguments(b);
      ft.commit();



      In that way the user won't have to move in another activity.



      And secondly I prefer Fragments because you can handle them easily during rotation.






      share|improve this answer





























        up vote
        1
        down vote













        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.






        share|improve this answer





























          up vote
          1
          down vote













          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






          share|improve this answer





















            protected by Pratik Butani Apr 16 at 5:55



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?














            13 Answers
            13






            active

            oldest

            votes








            13 Answers
            13






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            236
            down vote













            Experts will tell you: "When I see the UI, I will know whether to use an Activity or a Fragment". In the beginning this will not have any sense, but in time, you will actually be able to tell if you need Fragment or not.



            There is a good practice I found very helpful for me. It occurred to me while I was trying to explain something to my daughter.



            Namely, imagine a box which represents a screen. Can you load another screen in this box? If you use a new box, will you have to copy multiple items from the 1st box? If the answer is Yes, then you should use Fragments, because the root Activity can hold all duplicated elements to save you time in creating them, and you can simply replace parts of the box.



            But don't forget that you always need a box container (Activity) or your parts will be dispersed. So one box with parts inside.



            Take care not to misuse the box. Android UX experts advise (you can find them on YouTube) when we should explicitly load another Activity, instead to use a Fragment (like when we deal with the Navigation Drawer which has categories). Once you feel comfortable with Fragments, you can watch all their videos. Even more they are mandatory material.



            Can you right now look at your UI and figure out if you need an Activity or a Fragment? Did you get a new perspective? I think you did.






            share|improve this answer


















            • 4




              do you have a link to the youtube feed you mentioned? I search for "Android UX experts" and "Android UX" but am not entirely sure which videos you're talking about.
              – me--
              Jun 2 '16 at 8:15






            • 2




              Not any more, watched it over a year ago. Search for Android developer official talking about UX
              – sandalone
              Jun 2 '16 at 8:17






            • 1




              One example of consideration: activity has parentActivity so we can synthesize backstack while entering from notification, but I don't think there's such parentFragment.
              – fikr4n
              Feb 24 '17 at 22:51










            • @BornToCode there is getParentFragment: developer.android.com/reference/android/support/v4/app/…
              – ToolmakerSteve
              May 5 at 21:13











            • @ToolmakerSteve yes it's getParentFragment, but it's not what I meant dude, see developer.android.com/guide/topics/manifest/…
              – fikr4n
              May 14 at 7:42














            up vote
            236
            down vote













            Experts will tell you: "When I see the UI, I will know whether to use an Activity or a Fragment". In the beginning this will not have any sense, but in time, you will actually be able to tell if you need Fragment or not.



            There is a good practice I found very helpful for me. It occurred to me while I was trying to explain something to my daughter.



            Namely, imagine a box which represents a screen. Can you load another screen in this box? If you use a new box, will you have to copy multiple items from the 1st box? If the answer is Yes, then you should use Fragments, because the root Activity can hold all duplicated elements to save you time in creating them, and you can simply replace parts of the box.



            But don't forget that you always need a box container (Activity) or your parts will be dispersed. So one box with parts inside.



            Take care not to misuse the box. Android UX experts advise (you can find them on YouTube) when we should explicitly load another Activity, instead to use a Fragment (like when we deal with the Navigation Drawer which has categories). Once you feel comfortable with Fragments, you can watch all their videos. Even more they are mandatory material.



            Can you right now look at your UI and figure out if you need an Activity or a Fragment? Did you get a new perspective? I think you did.






            share|improve this answer


















            • 4




              do you have a link to the youtube feed you mentioned? I search for "Android UX experts" and "Android UX" but am not entirely sure which videos you're talking about.
              – me--
              Jun 2 '16 at 8:15






            • 2




              Not any more, watched it over a year ago. Search for Android developer official talking about UX
              – sandalone
              Jun 2 '16 at 8:17






            • 1




              One example of consideration: activity has parentActivity so we can synthesize backstack while entering from notification, but I don't think there's such parentFragment.
              – fikr4n
              Feb 24 '17 at 22:51










            • @BornToCode there is getParentFragment: developer.android.com/reference/android/support/v4/app/…
              – ToolmakerSteve
              May 5 at 21:13











            • @ToolmakerSteve yes it's getParentFragment, but it's not what I meant dude, see developer.android.com/guide/topics/manifest/…
              – fikr4n
              May 14 at 7:42












            up vote
            236
            down vote










            up vote
            236
            down vote









            Experts will tell you: "When I see the UI, I will know whether to use an Activity or a Fragment". In the beginning this will not have any sense, but in time, you will actually be able to tell if you need Fragment or not.



            There is a good practice I found very helpful for me. It occurred to me while I was trying to explain something to my daughter.



            Namely, imagine a box which represents a screen. Can you load another screen in this box? If you use a new box, will you have to copy multiple items from the 1st box? If the answer is Yes, then you should use Fragments, because the root Activity can hold all duplicated elements to save you time in creating them, and you can simply replace parts of the box.



            But don't forget that you always need a box container (Activity) or your parts will be dispersed. So one box with parts inside.



            Take care not to misuse the box. Android UX experts advise (you can find them on YouTube) when we should explicitly load another Activity, instead to use a Fragment (like when we deal with the Navigation Drawer which has categories). Once you feel comfortable with Fragments, you can watch all their videos. Even more they are mandatory material.



            Can you right now look at your UI and figure out if you need an Activity or a Fragment? Did you get a new perspective? I think you did.






            share|improve this answer














            Experts will tell you: "When I see the UI, I will know whether to use an Activity or a Fragment". In the beginning this will not have any sense, but in time, you will actually be able to tell if you need Fragment or not.



            There is a good practice I found very helpful for me. It occurred to me while I was trying to explain something to my daughter.



            Namely, imagine a box which represents a screen. Can you load another screen in this box? If you use a new box, will you have to copy multiple items from the 1st box? If the answer is Yes, then you should use Fragments, because the root Activity can hold all duplicated elements to save you time in creating them, and you can simply replace parts of the box.



            But don't forget that you always need a box container (Activity) or your parts will be dispersed. So one box with parts inside.



            Take care not to misuse the box. Android UX experts advise (you can find them on YouTube) when we should explicitly load another Activity, instead to use a Fragment (like when we deal with the Navigation Drawer which has categories). Once you feel comfortable with Fragments, you can watch all their videos. Even more they are mandatory material.



            Can you right now look at your UI and figure out if you need an Activity or a Fragment? Did you get a new perspective? I think you did.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 26 '17 at 15:48









            Kaushik NP

            4,53041948




            4,53041948










            answered Sep 23 '14 at 10:42









            sandalone

            22.7k52181293




            22.7k52181293







            • 4




              do you have a link to the youtube feed you mentioned? I search for "Android UX experts" and "Android UX" but am not entirely sure which videos you're talking about.
              – me--
              Jun 2 '16 at 8:15






            • 2




              Not any more, watched it over a year ago. Search for Android developer official talking about UX
              – sandalone
              Jun 2 '16 at 8:17






            • 1




              One example of consideration: activity has parentActivity so we can synthesize backstack while entering from notification, but I don't think there's such parentFragment.
              – fikr4n
              Feb 24 '17 at 22:51










            • @BornToCode there is getParentFragment: developer.android.com/reference/android/support/v4/app/…
              – ToolmakerSteve
              May 5 at 21:13











            • @ToolmakerSteve yes it's getParentFragment, but it's not what I meant dude, see developer.android.com/guide/topics/manifest/…
              – fikr4n
              May 14 at 7:42












            • 4




              do you have a link to the youtube feed you mentioned? I search for "Android UX experts" and "Android UX" but am not entirely sure which videos you're talking about.
              – me--
              Jun 2 '16 at 8:15






            • 2




              Not any more, watched it over a year ago. Search for Android developer official talking about UX
              – sandalone
              Jun 2 '16 at 8:17






            • 1




              One example of consideration: activity has parentActivity so we can synthesize backstack while entering from notification, but I don't think there's such parentFragment.
              – fikr4n
              Feb 24 '17 at 22:51










            • @BornToCode there is getParentFragment: developer.android.com/reference/android/support/v4/app/…
              – ToolmakerSteve
              May 5 at 21:13











            • @ToolmakerSteve yes it's getParentFragment, but it's not what I meant dude, see developer.android.com/guide/topics/manifest/…
              – fikr4n
              May 14 at 7:42







            4




            4




            do you have a link to the youtube feed you mentioned? I search for "Android UX experts" and "Android UX" but am not entirely sure which videos you're talking about.
            – me--
            Jun 2 '16 at 8:15




            do you have a link to the youtube feed you mentioned? I search for "Android UX experts" and "Android UX" but am not entirely sure which videos you're talking about.
            – me--
            Jun 2 '16 at 8:15




            2




            2




            Not any more, watched it over a year ago. Search for Android developer official talking about UX
            – sandalone
            Jun 2 '16 at 8:17




            Not any more, watched it over a year ago. Search for Android developer official talking about UX
            – sandalone
            Jun 2 '16 at 8:17




            1




            1




            One example of consideration: activity has parentActivity so we can synthesize backstack while entering from notification, but I don't think there's such parentFragment.
            – fikr4n
            Feb 24 '17 at 22:51




            One example of consideration: activity has parentActivity so we can synthesize backstack while entering from notification, but I don't think there's such parentFragment.
            – fikr4n
            Feb 24 '17 at 22:51












            @BornToCode there is getParentFragment: developer.android.com/reference/android/support/v4/app/…
            – ToolmakerSteve
            May 5 at 21:13





            @BornToCode there is getParentFragment: developer.android.com/reference/android/support/v4/app/…
            – ToolmakerSteve
            May 5 at 21:13













            @ToolmakerSteve yes it's getParentFragment, but it's not what I meant dude, see developer.android.com/guide/topics/manifest/…
            – fikr4n
            May 14 at 7:42




            @ToolmakerSteve yes it's getParentFragment, but it's not what I meant dude, see developer.android.com/guide/topics/manifest/…
            – fikr4n
            May 14 at 7:42












            up vote
            108
            down vote













            My philosophy is this:



            Create an Activity only if it's absolutely absolutely required. With the backstack made available for committing bunch of fragment transactions, I try to create as minimum of Activities in my app as possible. Also, communicating between various fragments is much easier rather than sending data back and forth between activities.



            Activity transitions are expensive, right? At least I believe so - since the old activity as to be destroyed/paused/stopped, pushed onto the stack and then the new activity has to be created/started/resumed.



            It's just my philosophy since fragments were introduced.






            share|improve this answer






















            • true, but as you've written, sometimes it is needed to use activities. one example is a camera screen, where it's better to use it in landscape mode. another example is the configuration screen that is shown when you put a customized appWidget (on the "desktop"- the launcher app).
              – android developer
              Nov 30 '13 at 23:22










            • Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
              – Emil Adz
              Dec 1 '13 at 12:02










            • @EmilAdz Yeah. I think so. Limit your app to use as minimum activities as possible - architect it in such a way if possible.
              – VJ Vélan Solutions
              Dec 1 '13 at 12:05










            • Then how do you solve the issue of fragments needing to pass eachother "state"? All of the state across all of your fragments need to live in the one activity, otherwise you're forced to use a singleton.
              – Mr_E
              Sep 27 '16 at 20:40






            • 13




              I'm not convinced that communicating between various fragments is much easier rather than sending data back and forth between activities.
              – Denny
              Mar 18 '17 at 23:11














            up vote
            108
            down vote













            My philosophy is this:



            Create an Activity only if it's absolutely absolutely required. With the backstack made available for committing bunch of fragment transactions, I try to create as minimum of Activities in my app as possible. Also, communicating between various fragments is much easier rather than sending data back and forth between activities.



            Activity transitions are expensive, right? At least I believe so - since the old activity as to be destroyed/paused/stopped, pushed onto the stack and then the new activity has to be created/started/resumed.



            It's just my philosophy since fragments were introduced.






            share|improve this answer






















            • true, but as you've written, sometimes it is needed to use activities. one example is a camera screen, where it's better to use it in landscape mode. another example is the configuration screen that is shown when you put a customized appWidget (on the "desktop"- the launcher app).
              – android developer
              Nov 30 '13 at 23:22










            • Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
              – Emil Adz
              Dec 1 '13 at 12:02










            • @EmilAdz Yeah. I think so. Limit your app to use as minimum activities as possible - architect it in such a way if possible.
              – VJ Vélan Solutions
              Dec 1 '13 at 12:05










            • Then how do you solve the issue of fragments needing to pass eachother "state"? All of the state across all of your fragments need to live in the one activity, otherwise you're forced to use a singleton.
              – Mr_E
              Sep 27 '16 at 20:40






            • 13




              I'm not convinced that communicating between various fragments is much easier rather than sending data back and forth between activities.
              – Denny
              Mar 18 '17 at 23:11












            up vote
            108
            down vote










            up vote
            108
            down vote









            My philosophy is this:



            Create an Activity only if it's absolutely absolutely required. With the backstack made available for committing bunch of fragment transactions, I try to create as minimum of Activities in my app as possible. Also, communicating between various fragments is much easier rather than sending data back and forth between activities.



            Activity transitions are expensive, right? At least I believe so - since the old activity as to be destroyed/paused/stopped, pushed onto the stack and then the new activity has to be created/started/resumed.



            It's just my philosophy since fragments were introduced.






            share|improve this answer














            My philosophy is this:



            Create an Activity only if it's absolutely absolutely required. With the backstack made available for committing bunch of fragment transactions, I try to create as minimum of Activities in my app as possible. Also, communicating between various fragments is much easier rather than sending data back and forth between activities.



            Activity transitions are expensive, right? At least I believe so - since the old activity as to be destroyed/paused/stopped, pushed onto the stack and then the new activity has to be created/started/resumed.



            It's just my philosophy since fragments were introduced.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 2 '15 at 5:25









            Gokul Nath KP

            7,6331859102




            7,6331859102










            answered Nov 30 '13 at 23:02









            VJ Vélan Solutions

            4,68743660




            4,68743660











            • true, but as you've written, sometimes it is needed to use activities. one example is a camera screen, where it's better to use it in landscape mode. another example is the configuration screen that is shown when you put a customized appWidget (on the "desktop"- the launcher app).
              – android developer
              Nov 30 '13 at 23:22










            • Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
              – Emil Adz
              Dec 1 '13 at 12:02










            • @EmilAdz Yeah. I think so. Limit your app to use as minimum activities as possible - architect it in such a way if possible.
              – VJ Vélan Solutions
              Dec 1 '13 at 12:05










            • Then how do you solve the issue of fragments needing to pass eachother "state"? All of the state across all of your fragments need to live in the one activity, otherwise you're forced to use a singleton.
              – Mr_E
              Sep 27 '16 at 20:40






            • 13




              I'm not convinced that communicating between various fragments is much easier rather than sending data back and forth between activities.
              – Denny
              Mar 18 '17 at 23:11
















            • true, but as you've written, sometimes it is needed to use activities. one example is a camera screen, where it's better to use it in landscape mode. another example is the configuration screen that is shown when you put a customized appWidget (on the "desktop"- the launcher app).
              – android developer
              Nov 30 '13 at 23:22










            • Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
              – Emil Adz
              Dec 1 '13 at 12:02










            • @EmilAdz Yeah. I think so. Limit your app to use as minimum activities as possible - architect it in such a way if possible.
              – VJ Vélan Solutions
              Dec 1 '13 at 12:05










            • Then how do you solve the issue of fragments needing to pass eachother "state"? All of the state across all of your fragments need to live in the one activity, otherwise you're forced to use a singleton.
              – Mr_E
              Sep 27 '16 at 20:40






            • 13




              I'm not convinced that communicating between various fragments is much easier rather than sending data back and forth between activities.
              – Denny
              Mar 18 '17 at 23:11















            true, but as you've written, sometimes it is needed to use activities. one example is a camera screen, where it's better to use it in landscape mode. another example is the configuration screen that is shown when you put a customized appWidget (on the "desktop"- the launcher app).
            – android developer
            Nov 30 '13 at 23:22




            true, but as you've written, sometimes it is needed to use activities. one example is a camera screen, where it's better to use it in landscape mode. another example is the configuration screen that is shown when you put a customized appWidget (on the "desktop"- the launcher app).
            – android developer
            Nov 30 '13 at 23:22












            Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
            – Emil Adz
            Dec 1 '13 at 12:02




            Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
            – Emil Adz
            Dec 1 '13 at 12:02












            @EmilAdz Yeah. I think so. Limit your app to use as minimum activities as possible - architect it in such a way if possible.
            – VJ Vélan Solutions
            Dec 1 '13 at 12:05




            @EmilAdz Yeah. I think so. Limit your app to use as minimum activities as possible - architect it in such a way if possible.
            – VJ Vélan Solutions
            Dec 1 '13 at 12:05












            Then how do you solve the issue of fragments needing to pass eachother "state"? All of the state across all of your fragments need to live in the one activity, otherwise you're forced to use a singleton.
            – Mr_E
            Sep 27 '16 at 20:40




            Then how do you solve the issue of fragments needing to pass eachother "state"? All of the state across all of your fragments need to live in the one activity, otherwise you're forced to use a singleton.
            – Mr_E
            Sep 27 '16 at 20:40




            13




            13




            I'm not convinced that communicating between various fragments is much easier rather than sending data back and forth between activities.
            – Denny
            Mar 18 '17 at 23:11




            I'm not convinced that communicating between various fragments is much easier rather than sending data back and forth between activities.
            – Denny
            Mar 18 '17 at 23:11










            up vote
            54
            down vote













            Well, according to Google's lectures (maybe here, I don't remember) , you should consider using Fragments whenever it's possible, as it makes your code easier to maintain and control.



            However, I think that on some cases it can get too complex, as the activity that hosts the fragments need to navigate/communicate between them.



            I think you should decide by yourself what's best for you. It's usually not that hard to convert an activity to a fragment and vice versa.



            I've created a post about this dillema here, if you wish to read some further.






            share|improve this answer


















            • 4




              Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
              – Emil Adz
              Dec 1 '13 at 12:02










            • It depends on the project, but if it gets too complicated to you, you can separate to multiple activities too. Don't be afraid to use any of the methods. You could also use them both. Maybe sometimes it would be too hard for you to use fragments instead of activities. I think you should try to use fragments, but don't force it to be everywhere if it gets too much in your way...
              – android developer
              Dec 1 '13 at 12:49











            • what if I want to keep this affect of the ActionBar staying intact and all the content is being switched? Is it possible to achieve this with Activities?
              – Emil Adz
              Dec 1 '13 at 13:08










            • What the Fragment? youtu.be/k3IT-IJ0J98?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC
              – Emer
              Oct 1 '16 at 0:04














            up vote
            54
            down vote













            Well, according to Google's lectures (maybe here, I don't remember) , you should consider using Fragments whenever it's possible, as it makes your code easier to maintain and control.



            However, I think that on some cases it can get too complex, as the activity that hosts the fragments need to navigate/communicate between them.



            I think you should decide by yourself what's best for you. It's usually not that hard to convert an activity to a fragment and vice versa.



            I've created a post about this dillema here, if you wish to read some further.






            share|improve this answer


















            • 4




              Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
              – Emil Adz
              Dec 1 '13 at 12:02










            • It depends on the project, but if it gets too complicated to you, you can separate to multiple activities too. Don't be afraid to use any of the methods. You could also use them both. Maybe sometimes it would be too hard for you to use fragments instead of activities. I think you should try to use fragments, but don't force it to be everywhere if it gets too much in your way...
              – android developer
              Dec 1 '13 at 12:49











            • what if I want to keep this affect of the ActionBar staying intact and all the content is being switched? Is it possible to achieve this with Activities?
              – Emil Adz
              Dec 1 '13 at 13:08










            • What the Fragment? youtu.be/k3IT-IJ0J98?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC
              – Emer
              Oct 1 '16 at 0:04












            up vote
            54
            down vote










            up vote
            54
            down vote









            Well, according to Google's lectures (maybe here, I don't remember) , you should consider using Fragments whenever it's possible, as it makes your code easier to maintain and control.



            However, I think that on some cases it can get too complex, as the activity that hosts the fragments need to navigate/communicate between them.



            I think you should decide by yourself what's best for you. It's usually not that hard to convert an activity to a fragment and vice versa.



            I've created a post about this dillema here, if you wish to read some further.






            share|improve this answer














            Well, according to Google's lectures (maybe here, I don't remember) , you should consider using Fragments whenever it's possible, as it makes your code easier to maintain and control.



            However, I think that on some cases it can get too complex, as the activity that hosts the fragments need to navigate/communicate between them.



            I think you should decide by yourself what's best for you. It's usually not that hard to convert an activity to a fragment and vice versa.



            I've created a post about this dillema here, if you wish to read some further.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:34









            Community

            11




            11










            answered Nov 30 '13 at 22:16









            android developer

            56.5k96457837




            56.5k96457837







            • 4




              Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
              – Emil Adz
              Dec 1 '13 at 12:02










            • It depends on the project, but if it gets too complicated to you, you can separate to multiple activities too. Don't be afraid to use any of the methods. You could also use them both. Maybe sometimes it would be too hard for you to use fragments instead of activities. I think you should try to use fragments, but don't force it to be everywhere if it gets too much in your way...
              – android developer
              Dec 1 '13 at 12:49











            • what if I want to keep this affect of the ActionBar staying intact and all the content is being switched? Is it possible to achieve this with Activities?
              – Emil Adz
              Dec 1 '13 at 13:08










            • What the Fragment? youtu.be/k3IT-IJ0J98?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC
              – Emer
              Oct 1 '16 at 0:04












            • 4




              Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
              – Emil Adz
              Dec 1 '13 at 12:02










            • It depends on the project, but if it gets too complicated to you, you can separate to multiple activities too. Don't be afraid to use any of the methods. You could also use them both. Maybe sometimes it would be too hard for you to use fragments instead of activities. I think you should try to use fragments, but don't force it to be everywhere if it gets too much in your way...
              – android developer
              Dec 1 '13 at 12:49











            • what if I want to keep this affect of the ActionBar staying intact and all the content is being switched? Is it possible to achieve this with Activities?
              – Emil Adz
              Dec 1 '13 at 13:08










            • What the Fragment? youtu.be/k3IT-IJ0J98?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC
              – Emer
              Oct 1 '16 at 0:04







            4




            4




            Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
            – Emil Adz
            Dec 1 '13 at 12:02




            Thanks for you answer and sharing you experience So you think it's a good practice in android to limit application to one Activity and use Fragment for all screen if the application architecture permits it?
            – Emil Adz
            Dec 1 '13 at 12:02












            It depends on the project, but if it gets too complicated to you, you can separate to multiple activities too. Don't be afraid to use any of the methods. You could also use them both. Maybe sometimes it would be too hard for you to use fragments instead of activities. I think you should try to use fragments, but don't force it to be everywhere if it gets too much in your way...
            – android developer
            Dec 1 '13 at 12:49





            It depends on the project, but if it gets too complicated to you, you can separate to multiple activities too. Don't be afraid to use any of the methods. You could also use them both. Maybe sometimes it would be too hard for you to use fragments instead of activities. I think you should try to use fragments, but don't force it to be everywhere if it gets too much in your way...
            – android developer
            Dec 1 '13 at 12:49













            what if I want to keep this affect of the ActionBar staying intact and all the content is being switched? Is it possible to achieve this with Activities?
            – Emil Adz
            Dec 1 '13 at 13:08




            what if I want to keep this affect of the ActionBar staying intact and all the content is being switched? Is it possible to achieve this with Activities?
            – Emil Adz
            Dec 1 '13 at 13:08












            What the Fragment? youtu.be/k3IT-IJ0J98?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC
            – Emer
            Oct 1 '16 at 0:04




            What the Fragment? youtu.be/k3IT-IJ0J98?list=PLOU2XLYxmsILe6_eGvDN3GyiodoV3qNSC
            – Emer
            Oct 1 '16 at 0:04










            up vote
            22
            down vote













            Why I prefer Fragment over Activity in ALL CASES.



            • Activity is expensive. In Fragment, views and property states are separated - whenever a fragment is in backstack, its views will be destroyed. So you can stack much more Fragments than Activity.


            • Backstack manipulation. With FragmentManager, it's easy to clear all the Fragments, insert more than on Fragments and etcs. But for Activity, it will be a nightmare to manipulate those stuff.


            • A much predictable lifecycle. As long as the host Activity is not recycled. the Fragments in the backstack will not be recycled. So it's possible to use FragmentManager::getFragments() to find specific Fragment (not encouraged).






            share|improve this answer






















            • HI, I read your review about the advantages of Frag over Act, Do you have any project to show the same in your Github Repo?
              – Ümañg ßürmån
              Nov 15 at 5:53














            up vote
            22
            down vote













            Why I prefer Fragment over Activity in ALL CASES.



            • Activity is expensive. In Fragment, views and property states are separated - whenever a fragment is in backstack, its views will be destroyed. So you can stack much more Fragments than Activity.


            • Backstack manipulation. With FragmentManager, it's easy to clear all the Fragments, insert more than on Fragments and etcs. But for Activity, it will be a nightmare to manipulate those stuff.


            • A much predictable lifecycle. As long as the host Activity is not recycled. the Fragments in the backstack will not be recycled. So it's possible to use FragmentManager::getFragments() to find specific Fragment (not encouraged).






            share|improve this answer






















            • HI, I read your review about the advantages of Frag over Act, Do you have any project to show the same in your Github Repo?
              – Ümañg ßürmån
              Nov 15 at 5:53












            up vote
            22
            down vote










            up vote
            22
            down vote









            Why I prefer Fragment over Activity in ALL CASES.



            • Activity is expensive. In Fragment, views and property states are separated - whenever a fragment is in backstack, its views will be destroyed. So you can stack much more Fragments than Activity.


            • Backstack manipulation. With FragmentManager, it's easy to clear all the Fragments, insert more than on Fragments and etcs. But for Activity, it will be a nightmare to manipulate those stuff.


            • A much predictable lifecycle. As long as the host Activity is not recycled. the Fragments in the backstack will not be recycled. So it's possible to use FragmentManager::getFragments() to find specific Fragment (not encouraged).






            share|improve this answer














            Why I prefer Fragment over Activity in ALL CASES.



            • Activity is expensive. In Fragment, views and property states are separated - whenever a fragment is in backstack, its views will be destroyed. So you can stack much more Fragments than Activity.


            • Backstack manipulation. With FragmentManager, it's easy to clear all the Fragments, insert more than on Fragments and etcs. But for Activity, it will be a nightmare to manipulate those stuff.


            • A much predictable lifecycle. As long as the host Activity is not recycled. the Fragments in the backstack will not be recycled. So it's possible to use FragmentManager::getFragments() to find specific Fragment (not encouraged).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 29 '16 at 3:13









            Charuක

            9,81152968




            9,81152968










            answered Dec 3 '15 at 12:47









            Qylin

            818821




            818821











            • HI, I read your review about the advantages of Frag over Act, Do you have any project to show the same in your Github Repo?
              – Ümañg ßürmån
              Nov 15 at 5:53
















            • HI, I read your review about the advantages of Frag over Act, Do you have any project to show the same in your Github Repo?
              – Ümañg ßürmån
              Nov 15 at 5:53















            HI, I read your review about the advantages of Frag over Act, Do you have any project to show the same in your Github Repo?
            – Ümañg ßürmån
            Nov 15 at 5:53




            HI, I read your review about the advantages of Frag over Act, Do you have any project to show the same in your Github Repo?
            – Ümañg ßürmån
            Nov 15 at 5:53










            up vote
            12
            down vote













            In my opinion it's not really relevant. The key factor to consider is



            1. how often are you gonna reuse parts of the UI (menus for example),

            2. is the app also for tablets?

            The main use of fragments is to build multipane activities, which makes it perfect for Tablet/Phone responsive apps.






            share|improve this answer






















            • I would say the main use of fragments is to make custom views without thinking of them as custom views. that's what happens anyways. Fragments we're originally shown from Google as a handy way to make tablet responsive apps, so you could stick them in different activities if you wanted. a way to attach code to a view, more or less, and have them stickable where you want(without making custom views).
              – Lassi Kinnunen
              Feb 28 at 15:36














            up vote
            12
            down vote













            In my opinion it's not really relevant. The key factor to consider is



            1. how often are you gonna reuse parts of the UI (menus for example),

            2. is the app also for tablets?

            The main use of fragments is to build multipane activities, which makes it perfect for Tablet/Phone responsive apps.






            share|improve this answer






















            • I would say the main use of fragments is to make custom views without thinking of them as custom views. that's what happens anyways. Fragments we're originally shown from Google as a handy way to make tablet responsive apps, so you could stick them in different activities if you wanted. a way to attach code to a view, more or less, and have them stickable where you want(without making custom views).
              – Lassi Kinnunen
              Feb 28 at 15:36












            up vote
            12
            down vote










            up vote
            12
            down vote









            In my opinion it's not really relevant. The key factor to consider is



            1. how often are you gonna reuse parts of the UI (menus for example),

            2. is the app also for tablets?

            The main use of fragments is to build multipane activities, which makes it perfect for Tablet/Phone responsive apps.






            share|improve this answer














            In my opinion it's not really relevant. The key factor to consider is



            1. how often are you gonna reuse parts of the UI (menus for example),

            2. is the app also for tablets?

            The main use of fragments is to build multipane activities, which makes it perfect for Tablet/Phone responsive apps.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 29 '16 at 3:12









            Charuක

            9,81152968




            9,81152968










            answered Mar 12 '16 at 22:41









            Isaac Urbina

            887618




            887618











            • I would say the main use of fragments is to make custom views without thinking of them as custom views. that's what happens anyways. Fragments we're originally shown from Google as a handy way to make tablet responsive apps, so you could stick them in different activities if you wanted. a way to attach code to a view, more or less, and have them stickable where you want(without making custom views).
              – Lassi Kinnunen
              Feb 28 at 15:36
















            • I would say the main use of fragments is to make custom views without thinking of them as custom views. that's what happens anyways. Fragments we're originally shown from Google as a handy way to make tablet responsive apps, so you could stick them in different activities if you wanted. a way to attach code to a view, more or less, and have them stickable where you want(without making custom views).
              – Lassi Kinnunen
              Feb 28 at 15:36















            I would say the main use of fragments is to make custom views without thinking of them as custom views. that's what happens anyways. Fragments we're originally shown from Google as a handy way to make tablet responsive apps, so you could stick them in different activities if you wanted. a way to attach code to a view, more or less, and have them stickable where you want(without making custom views).
            – Lassi Kinnunen
            Feb 28 at 15:36




            I would say the main use of fragments is to make custom views without thinking of them as custom views. that's what happens anyways. Fragments we're originally shown from Google as a handy way to make tablet responsive apps, so you could stick them in different activities if you wanted. a way to attach code to a view, more or less, and have them stickable where you want(without making custom views).
            – Lassi Kinnunen
            Feb 28 at 15:36










            up vote
            8
            down vote













            There's more to this than you realize, you have to remember than an activity that is launched does not implicitly destroy the calling activity. Sure, you can set it up such that your user clicks a button to go to a page, you start that page's activity and destroy the current one. This causes a lot of overhead. The best guide I can give you is:



            ** Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows).



            A great example of when it makes sense to have multiple activities is Google Drive. The main activity provides a file explorer. When a file is opened, a new activity is launched to view that file. You can press the recent apps button which will allow you to go back to the browser without closing the opened document, then perhaps even open another document in parallel to the first.






            share|improve this answer




















            • Re "Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows)." I don't think so. That situation is well solved using fragments attach/detach methods.
              – ToolmakerSteve
              Sep 20 '16 at 16:17














            up vote
            8
            down vote













            There's more to this than you realize, you have to remember than an activity that is launched does not implicitly destroy the calling activity. Sure, you can set it up such that your user clicks a button to go to a page, you start that page's activity and destroy the current one. This causes a lot of overhead. The best guide I can give you is:



            ** Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows).



            A great example of when it makes sense to have multiple activities is Google Drive. The main activity provides a file explorer. When a file is opened, a new activity is launched to view that file. You can press the recent apps button which will allow you to go back to the browser without closing the opened document, then perhaps even open another document in parallel to the first.






            share|improve this answer




















            • Re "Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows)." I don't think so. That situation is well solved using fragments attach/detach methods.
              – ToolmakerSteve
              Sep 20 '16 at 16:17












            up vote
            8
            down vote










            up vote
            8
            down vote









            There's more to this than you realize, you have to remember than an activity that is launched does not implicitly destroy the calling activity. Sure, you can set it up such that your user clicks a button to go to a page, you start that page's activity and destroy the current one. This causes a lot of overhead. The best guide I can give you is:



            ** Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows).



            A great example of when it makes sense to have multiple activities is Google Drive. The main activity provides a file explorer. When a file is opened, a new activity is launched to view that file. You can press the recent apps button which will allow you to go back to the browser without closing the opened document, then perhaps even open another document in parallel to the first.






            share|improve this answer












            There's more to this than you realize, you have to remember than an activity that is launched does not implicitly destroy the calling activity. Sure, you can set it up such that your user clicks a button to go to a page, you start that page's activity and destroy the current one. This causes a lot of overhead. The best guide I can give you is:



            ** Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows).



            A great example of when it makes sense to have multiple activities is Google Drive. The main activity provides a file explorer. When a file is opened, a new activity is launched to view that file. You can press the recent apps button which will allow you to go back to the browser without closing the opened document, then perhaps even open another document in parallel to the first.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 5 '15 at 14:27









            TheHebrewHammer

            1,1051920




            1,1051920











            • Re "Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows)." I don't think so. That situation is well solved using fragments attach/detach methods.
              – ToolmakerSteve
              Sep 20 '16 at 16:17
















            • Re "Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows)." I don't think so. That situation is well solved using fragments attach/detach methods.
              – ToolmakerSteve
              Sep 20 '16 at 16:17















            Re "Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows)." I don't think so. That situation is well solved using fragments attach/detach methods.
            – ToolmakerSteve
            Sep 20 '16 at 16:17




            Re "Start a new activity only if it makes sense to have the main activity and this one open at the same time (think of multiple windows)." I don't think so. That situation is well solved using fragments attach/detach methods.
            – ToolmakerSteve
            Sep 20 '16 at 16:17










            up vote
            7
            down vote













            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.






            share|improve this answer


























              up vote
              7
              down vote













              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.






              share|improve this answer
























                up vote
                7
                down vote










                up vote
                7
                down vote









                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.






                share|improve this answer














                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 2 '15 at 16:57

























                answered Feb 2 '15 at 16:15









                guest

                8112




                8112




















                    up vote
                    7
                    down vote













                    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, that

                      will hard to handle. Ex: when adding new fragment, you can change action bar title, but when pop it from backstack 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.

                    • When we need startForResult, activity has but fragment hasn't.

                    • Don't have transition animation by default

                    My solution for this is using an Activity to wrap a fragment inside. So we have separate action bar, menu, startActivityForResult, animation,...






                    share|improve this answer


















                    • 1




                      Very useful points, thanks. Can you clarify "an Activity to wrap a fragment"? Did you make a separate Activity for each fragment? If so, do you need Fragment at all?
                      – ToolmakerSteve
                      Sep 20 '16 at 15:24







                    • 1




                      there is a way to restore title and stuff. use getSupportFragmentManager().addOnBackStackChangedListener to add a listener. get current fragment in that listener and then set title and stuff.
                      – babay
                      Sep 9 '17 at 2:18














                    up vote
                    7
                    down vote













                    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, that

                      will hard to handle. Ex: when adding new fragment, you can change action bar title, but when pop it from backstack 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.

                    • When we need startForResult, activity has but fragment hasn't.

                    • Don't have transition animation by default

                    My solution for this is using an Activity to wrap a fragment inside. So we have separate action bar, menu, startActivityForResult, animation,...






                    share|improve this answer


















                    • 1




                      Very useful points, thanks. Can you clarify "an Activity to wrap a fragment"? Did you make a separate Activity for each fragment? If so, do you need Fragment at all?
                      – ToolmakerSteve
                      Sep 20 '16 at 15:24







                    • 1




                      there is a way to restore title and stuff. use getSupportFragmentManager().addOnBackStackChangedListener to add a listener. get current fragment in that listener and then set title and stuff.
                      – babay
                      Sep 9 '17 at 2:18












                    up vote
                    7
                    down vote










                    up vote
                    7
                    down vote









                    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, that

                      will hard to handle. Ex: when adding new fragment, you can change action bar title, but when pop it from backstack 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.

                    • When we need startForResult, activity has but fragment hasn't.

                    • Don't have transition animation by default

                    My solution for this is using an Activity to wrap a fragment inside. So we have separate action bar, menu, startActivityForResult, animation,...






                    share|improve this answer














                    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, that

                      will hard to handle. Ex: when adding new fragment, you can change action bar title, but when pop it from backstack 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.

                    • When we need startForResult, activity has but fragment hasn't.

                    • Don't have transition animation by default

                    My solution for this is using an Activity to wrap a fragment inside. So we have separate action bar, menu, startActivityForResult, animation,...







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 29 '16 at 3:14









                    Charuක

                    9,81152968




                    9,81152968










                    answered Nov 18 '15 at 3:01









                    I Love Coding

                    26556




                    26556







                    • 1




                      Very useful points, thanks. Can you clarify "an Activity to wrap a fragment"? Did you make a separate Activity for each fragment? If so, do you need Fragment at all?
                      – ToolmakerSteve
                      Sep 20 '16 at 15:24







                    • 1




                      there is a way to restore title and stuff. use getSupportFragmentManager().addOnBackStackChangedListener to add a listener. get current fragment in that listener and then set title and stuff.
                      – babay
                      Sep 9 '17 at 2:18












                    • 1




                      Very useful points, thanks. Can you clarify "an Activity to wrap a fragment"? Did you make a separate Activity for each fragment? If so, do you need Fragment at all?
                      – ToolmakerSteve
                      Sep 20 '16 at 15:24







                    • 1




                      there is a way to restore title and stuff. use getSupportFragmentManager().addOnBackStackChangedListener to add a listener. get current fragment in that listener and then set title and stuff.
                      – babay
                      Sep 9 '17 at 2:18







                    1




                    1




                    Very useful points, thanks. Can you clarify "an Activity to wrap a fragment"? Did you make a separate Activity for each fragment? If so, do you need Fragment at all?
                    – ToolmakerSteve
                    Sep 20 '16 at 15:24





                    Very useful points, thanks. Can you clarify "an Activity to wrap a fragment"? Did you make a separate Activity for each fragment? If so, do you need Fragment at all?
                    – ToolmakerSteve
                    Sep 20 '16 at 15:24





                    1




                    1




                    there is a way to restore title and stuff. use getSupportFragmentManager().addOnBackStackChangedListener to add a listener. get current fragment in that listener and then set title and stuff.
                    – babay
                    Sep 9 '17 at 2:18




                    there is a way to restore title and stuff. use getSupportFragmentManager().addOnBackStackChangedListener to add a listener. get current fragment in that listener and then set title and stuff.
                    – babay
                    Sep 9 '17 at 2:18










                    up vote
                    3
                    down vote













                    The one big advantage of a fragment over activity is that , the code which is used for fragment can be used for different activities.so, it provides re-usability of code in application development.






                    share|improve this answer


















                    • 3




                      How? COuld you provide some example please?
                      – sofs1
                      Dec 24 '16 at 11:03














                    up vote
                    3
                    down vote













                    The one big advantage of a fragment over activity is that , the code which is used for fragment can be used for different activities.so, it provides re-usability of code in application development.






                    share|improve this answer


















                    • 3




                      How? COuld you provide some example please?
                      – sofs1
                      Dec 24 '16 at 11:03












                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    The one big advantage of a fragment over activity is that , the code which is used for fragment can be used for different activities.so, it provides re-usability of code in application development.






                    share|improve this answer














                    The one big advantage of a fragment over activity is that , the code which is used for fragment can be used for different activities.so, it provides re-usability of code in application development.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 29 '16 at 3:12









                    Charuක

                    9,81152968




                    9,81152968










                    answered Jun 9 '16 at 19:38









                    Sanchit Bhasin

                    312




                    312







                    • 3




                      How? COuld you provide some example please?
                      – sofs1
                      Dec 24 '16 at 11:03












                    • 3




                      How? COuld you provide some example please?
                      – sofs1
                      Dec 24 '16 at 11:03







                    3




                    3




                    How? COuld you provide some example please?
                    – sofs1
                    Dec 24 '16 at 11:03




                    How? COuld you provide some example please?
                    – sofs1
                    Dec 24 '16 at 11:03










                    up vote
                    2
                    down vote













                    You are free to use one of those.

                    Basically, you have to evaluate which is the best one to your app. Think about how you will manage the business flow and how to store/manage data preferences.



                    Think about, how Fragments store garbage data. When you implement the fragment, you have a activity root to fill with fragment(s). So, if your trying to implement a lot of activities with too much fragments, you have to consider performance on your app, coz you're manipulating (coarsely speaks) two context lifecycle, remember the complexity.



                    Remember: should I use fragments? Why shouldn't I?



                    regards.






                    share|improve this answer
























                      up vote
                      2
                      down vote













                      You are free to use one of those.

                      Basically, you have to evaluate which is the best one to your app. Think about how you will manage the business flow and how to store/manage data preferences.



                      Think about, how Fragments store garbage data. When you implement the fragment, you have a activity root to fill with fragment(s). So, if your trying to implement a lot of activities with too much fragments, you have to consider performance on your app, coz you're manipulating (coarsely speaks) two context lifecycle, remember the complexity.



                      Remember: should I use fragments? Why shouldn't I?



                      regards.






                      share|improve this answer






















                        up vote
                        2
                        down vote










                        up vote
                        2
                        down vote









                        You are free to use one of those.

                        Basically, you have to evaluate which is the best one to your app. Think about how you will manage the business flow and how to store/manage data preferences.



                        Think about, how Fragments store garbage data. When you implement the fragment, you have a activity root to fill with fragment(s). So, if your trying to implement a lot of activities with too much fragments, you have to consider performance on your app, coz you're manipulating (coarsely speaks) two context lifecycle, remember the complexity.



                        Remember: should I use fragments? Why shouldn't I?



                        regards.






                        share|improve this answer












                        You are free to use one of those.

                        Basically, you have to evaluate which is the best one to your app. Think about how you will manage the business flow and how to store/manage data preferences.



                        Think about, how Fragments store garbage data. When you implement the fragment, you have a activity root to fill with fragment(s). So, if your trying to implement a lot of activities with too much fragments, you have to consider performance on your app, coz you're manipulating (coarsely speaks) two context lifecycle, remember the complexity.



                        Remember: should I use fragments? Why shouldn't I?



                        regards.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Sep 28 '17 at 23:19









                        Franklin Hirata

                        232211




                        232211




















                            up vote
                            1
                            down vote













                            I use Fragments for better user experience. For example if you have a Button and you want to run let's say a webservice when you click it, I attach a Fragment to the parent Activity.



                            if (id == R.id.forecast) 

                            ForecastFragment forecastFragment = new ForecastFragment();
                            FragmentManager fm = getSupportFragmentManager();
                            FragmentTransaction ft = fm.beginTransaction();
                            ft.replace(R.id.main_content, forecastFragment);
                            ft.addToBackStack("backstack");
                            forecastFragment.setArguments(b);
                            ft.commit();



                            In that way the user won't have to move in another activity.



                            And secondly I prefer Fragments because you can handle them easily during rotation.






                            share|improve this answer


























                              up vote
                              1
                              down vote













                              I use Fragments for better user experience. For example if you have a Button and you want to run let's say a webservice when you click it, I attach a Fragment to the parent Activity.



                              if (id == R.id.forecast) 

                              ForecastFragment forecastFragment = new ForecastFragment();
                              FragmentManager fm = getSupportFragmentManager();
                              FragmentTransaction ft = fm.beginTransaction();
                              ft.replace(R.id.main_content, forecastFragment);
                              ft.addToBackStack("backstack");
                              forecastFragment.setArguments(b);
                              ft.commit();



                              In that way the user won't have to move in another activity.



                              And secondly I prefer Fragments because you can handle them easily during rotation.






                              share|improve this answer
























                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                I use Fragments for better user experience. For example if you have a Button and you want to run let's say a webservice when you click it, I attach a Fragment to the parent Activity.



                                if (id == R.id.forecast) 

                                ForecastFragment forecastFragment = new ForecastFragment();
                                FragmentManager fm = getSupportFragmentManager();
                                FragmentTransaction ft = fm.beginTransaction();
                                ft.replace(R.id.main_content, forecastFragment);
                                ft.addToBackStack("backstack");
                                forecastFragment.setArguments(b);
                                ft.commit();



                                In that way the user won't have to move in another activity.



                                And secondly I prefer Fragments because you can handle them easily during rotation.






                                share|improve this answer














                                I use Fragments for better user experience. For example if you have a Button and you want to run let's say a webservice when you click it, I attach a Fragment to the parent Activity.



                                if (id == R.id.forecast) 

                                ForecastFragment forecastFragment = new ForecastFragment();
                                FragmentManager fm = getSupportFragmentManager();
                                FragmentTransaction ft = fm.beginTransaction();
                                ft.replace(R.id.main_content, forecastFragment);
                                ft.addToBackStack("backstack");
                                forecastFragment.setArguments(b);
                                ft.commit();



                                In that way the user won't have to move in another activity.



                                And secondly I prefer Fragments because you can handle them easily during rotation.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 28 '16 at 19:00









                                Massimiliano Kraus

                                2,33741532




                                2,33741532










                                answered May 29 '16 at 8:37









                                Theo

                                1,30322844




                                1,30322844




















                                    up vote
                                    1
                                    down vote













                                    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.






                                    share|improve this answer


























                                      up vote
                                      1
                                      down vote













                                      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.






                                      share|improve this answer
























                                        up vote
                                        1
                                        down vote










                                        up vote
                                        1
                                        down vote









                                        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.






                                        share|improve this answer














                                        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.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Dec 29 '16 at 3:07









                                        Charuක

                                        9,81152968




                                        9,81152968










                                        answered Dec 21 '16 at 19:06









                                        Theo

                                        1,30322844




                                        1,30322844




















                                            up vote
                                            1
                                            down vote













                                            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






                                            share|improve this answer


























                                              up vote
                                              1
                                              down vote













                                              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






                                              share|improve this answer
























                                                up vote
                                                1
                                                down vote










                                                up vote
                                                1
                                                down vote









                                                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






                                                share|improve this answer














                                                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







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Dec 29 '16 at 3:08









                                                Charuක

                                                9,81152968




                                                9,81152968










                                                answered Sep 9 '16 at 0:53









                                                varg

                                                211




                                                211















                                                    protected by Pratik Butani Apr 16 at 5:55



                                                    Thank you for your interest in this question.
                                                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                    Would you like to answer one of these unanswered questions instead?



                                                    Popular posts from this blog

                                                    27

                                                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                                                    Category:Rhetoric