To Fragment or not to Fragment - Nested Fragments against Activities. Why should I use more than one Activity?

前端 未结 5 1449
太阳男子
太阳男子 2020-12-12 09:41

There are a lot of discussions about whether you should use Activities or Fragments. For example:

  • To-Fragment-or-not-to-Fragment
5条回答
  •  臣服心动
    2020-12-12 10:40

    For your questions i can only say that sometimes, with a complex User Experience or complex app that must use a different hardware component you need to use activity instead of fragment.

    For example, if you have to create an app with some form that have a step that take a photo and then use this photo for some work hard memory task ( face detection for example ) that use memory you need to separate this task from the main task activity and personalise the permission on manifest to use more memory only on this activity.

    Another example is if you want to use weak memory activity ( in manifest you can set properties that the clear the activity stack and force the Garbage Collector to clean memory when the activity finish )

    The most probably situation that requires to use more activity and fragment is the User Experience of the app. If you need a right drawer custom that contains a menu and every fragment of the menu contains a listView and every list view must go in a detail. You can do a lot of trick to hide the right drawer and create animation to slide from fragment to the detail, but the best and simplest way is to make a new activity for the detail and manage it with a separate logic/lifecycle from the main activity with the drawer. I've done this choose in two of my apps:

    iMeal - https://play.google.com/store/apps/details?id=it.fullsix.chiccopappe

    GGRugby - https://play.google.com/store/apps/details?id=it.f6.template

    In this app the Drawer is in the Main Fragment Activity, every menu change the content fragment. The list view in the content fragment change activity context with intent and go in another activity.

    Finally there are some situation that i think you should work with more that one only fragmentActivity...but this a my pattern of work, probably you can find a trick/way or scope to do something with only an Activity and fragment.

提交回复
热议问题