How to choose "Activity" or "Fragment" if both scenarios are possible?

In an Android app similar to the app market, I have a Home view and two other views contain lists. All three of these lists will go into the product list, but with different arguments. And another view shows the presentation of product information.

In such a scenario, when should I use fragments and when should I use actions? And how to determine? knowing that I am looking for a text box on the toolbar that should appear throughout the system.

The options that I have now is one action in the entire application that contains a search toolbar. This activity will help in navigating between fragments. Another option is to set the first 3 views in action, as well as a list of products in another action and product details in a separate one.

+4
source share
1 answer

The reason fragments were created was to allow modular layout options on larger screen devices. A classic example is a dual-panel layout that adapts to screen size.

dual pane layout

So, the types of questions you can ask yourself to determine if fragments are a good choice for you are:

  • I want my application to adapt its appearance based on screen size?
  • Is it possible to use a larger screen device to display 2, 3, 4 fragments on one screen?
  • Am I duplicating the same interface elements in all my actions?

If so, then fragments are what you are looking for. If not, you can use actions.

Fragments are, of course, the future. In your example, your basic activity, which contains all of your fragments, may contain a search bar and can handle the attachment of new fragments and act as the average person to send data between fragments.

+7
source

Source: https://habr.com/ru/post/1440827/


All Articles