Multiple activity or single activity and several fragments

I have one very general question, I did not find a specific answer to my question, so I returned it again.

I want to decide between two approaches

  • Dedicated actions for various screens and tasks to avoid difficulties and problems.
  • Single actions and several fragments for different tasks, and the user can move around as a fragment of activity. The user will be moved to fragment B, fragment C, this may be a transaction back and forth.

What do I want to know?

  • Is activity transition so expensive for the processor, or is it insignificant to achieve memory simplicity?
  • Does the snippet have the overhead of transition lifecycle management so that all problems can arise with this lifecycle management?
  • How easy is it to handle a fragment transaction while maintaining the fragment state?

We do not know now how much data will be there to store the fragment.

+5
source share
4 answers

Well, it completely depends on the application design, flow and navigation.

Here are some of the benefits of using Single Activity and Multiple Fragments:

  • Performance fragment operations are faster than creating new actions.
  • The navigation box and toolbar are easily controlled with Single activity.
  • The same context can be used everywhere.
  • The setRetainInstance snippet is very useful in managing orientation changes.

with this, there are several disadvantages:

  • Activity gets very dirty with lots of code.
  • Pressing the start button back is tedious, since only Activity can process non-fragments.

I personally use several actions with several fragments in which I separate module-based actions. In the same module, submodules can be created in fragments. It was easy for me to manage in different scenarios, as if the application was closing, opening again, in notifications, orientation changes.

+5
source

My question is the same, so far there is only one action in my application, and the rest are Fragments. I agree that maintaining a fragment is difficult, but using Fragment will increase your performance.

Suppose we take one example,

I have 10 types of activities, in each event I call the Async task to perform some background operations. In each Async onPostExecute () task, you update your interface. But before doing doInBackground (), you switched Activity and this activity was destroyed, but remember that doInBackground () is still running, and as soon as it is completed, onPostExecute will be called, and in onPostExecute () we update the user interface, but the activity is destroyed , so this will create a leak in your application. But if you support only one action, then it will be easy to support.

Waiting for the opinions of others.

+1
source

In addition to the answers above, a few points that I would add that can help in deciding when to use the fragment and when to use the action.

  • If you support large screen sizes, fragments are definitely preferable to actions.
  • If you have code that you think can be reused on multiple screens, you can use a snippet that can be reused in different places.
+1
source

A small point to add in support of "single multiple activity fragments."

Sometimes you may need a component (for example, a banner ad) to save between different pages / screens of your application. If you have several Activities , you will need to recreate this component on each of the Activities . But if you have one Activity , you just add this component to the Activity layout and you can forget about it.

0
source

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


All Articles