Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Activity or Single Activity and multiple fragments

I have one very general question, I did not find a concrete answer for my question hence putting it again.

I want to decide between two approaches

  1. Dedicated activities for various various screens and tasks to avoid complexity and issues
  2. Single Activities and multiple fragments for different tasks and user can navigate like Activity holding Fragment A user will navigate to Fragment B, Fragment C , this can be back and forth transaction.

What I want to know?

  1. Is Activity transition is that costly for processor or to achieve simplicity memory overhead is negligible ?
  2. Fragment has overhead of managing life cycle with transition, so what all problem can come with this life cycler management?
  3. How easy is to deal with fragment transaction with saving state of the fragment?

We don't know right now what amount data will be there for fragment to hold.

like image 520
Hardik Trivedi Avatar asked Jul 03 '17 08:07

Hardik Trivedi


3 Answers

Well, it totally depends on the application's design, flow and it's navigation.

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

  1. Performance fragment transactions are fast than creating new activities.
  2. Navigation Drawer and Toolbar, it's easy to manage with Single activity.
  3. Same Context can be used everywhere.
  4. Fragment's setRetainInstance is very helpful while managing orientation changes.

with it, here comes few drawbacks:

  1. Activity gets really messy with a lots of code.
  2. Handling button backpress is tedious as only Activity can handle that not Fragments.

I personally use Multiple activities with multiple fragments in which I separate activities based on the modules. In same module, submodules can be created in fragments. I found it easy to manage in different scenarios as if application gets closed, reopens, in notifications, orientation changes.

like image 91
Ani Avatar answered Oct 13 '22 03:10

Ani


My question is same, till now in my all app, there is only one Activity, and the rests are Fragments. I agree it is hard to maintain Fragment, but using Fragment will increase your Performance.

Suppose, take one example,

I have 10 Activities, in each Activity, I'm calling Async Task to perform some background operations. In each Async Task's onPostExecute() you are updating your UI. But before completing the doInBackground() you switched the Activity and that Activity is destroyed, but remember the doInBackground() is still in progress, and once it is finished, onPostExecute will be called, and in onPostExecute() we are updating the UI, but the Activity is destroyed, so this will create a leak in your app. But if you are maintaining only one Activity then it will be easy to maintain.

Waiting for others opinion also.

like image 5
Aman Shekhar Avatar answered Oct 13 '22 03:10

Aman Shekhar


Apart from answers above, few points which I would add which can help in deciding when to use a Fragment and when to use an Activity.

  • If you're supporting larger screen sizes, fragments are definitely preferred over activities.
  • When you've some code which you think can be reusable in multiple screens, you can use a fragment which can be reused across different places
like image 3
Mahendra Liya Avatar answered Oct 13 '22 04:10

Mahendra Liya