Dilemma: when to use Fragments vs Activities:

后端 未结 14 697
失恋的感觉
失恋的感觉 2020-11-22 11:58

I know that Activities are designed to represent a single screen of my application, while Fragments are designed to be reusable UI layouts with log

相关标签:
14条回答
  • 2020-11-22 12:03

    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.

    0 讨论(0)
  • 2020-11-22 12:06

    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.

    0 讨论(0)
  • 2020-11-22 12:06

    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.

    0 讨论(0)
  • 2020-11-22 12:07

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

    0 讨论(0)
  • 2020-11-22 12:08

    Since Jetpack, Single-Activity app is the preferred architecture. Usefull especially with the Navigation Architecture Component.

    source

    0 讨论(0)
  • 2020-11-22 12:09

    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

    0 讨论(0)
提交回复
热议问题