1

I've been developing Android for 3 years, and this is still a hard thing to decide when it comes to the similar scenario. Nowadays, I believe most of the applications has a multi-step screen / flow.

For example, a booking flow. You need to proceed through several steps like select ticket, select seat, select product details, then enter personal details. Each step might need some data from the previous step.

I did a lot of researches on this, and read a lot of posts from reddit and SO, but still don't come to a conclusion.

Google told us to use fragments for flexible UI, and Jake Wharton also mentioned that 'You can use fragment, but don't use its backstack.'

Should i use single activity with multiple fragments as steps, or separate activities, with each activity representing a step?

Currently, i am using the first approach, by adding fragment into backstack, and restore their data state when popping (user press back). However, i need to set 'freezesText' on all textviews and make sure recyclerview adapter is in the fragment onCreate to preserve its state. There might be more pains incoming.

I even wrote a custom fragment container / manager to maintain my fragments seamlessly.

https://github.com/Veeyikpong/easyfragmentcontainer/blob/master/src/main/java/com/veeyikpong/easyfragmentcontainer/FragmentContainer.kt

I see many sources mentioning that activity transitions are more expensive. Thus, I wrote two apps simulating the scenario to compare the performance. Both apps served the same screen and result, developed with MVP arch. These apps are pulling news into a list, and u can press them to view detail.

Single activity with two fragments: https://github.com/Veeyikpong/fastNewsFragment

Separate activities: https://github.com/Veeyikpong/fastNewsActivity

I compared them with Android studio profiler, noticed theres not much different in memory consumption, but the separate activities app have lesser code size (10216kb) and the one with fragments is 14000kb.

FastNewsActivity FastNewsActivity

FastNewsFragment FastNewsFragment

Based on the test, i didn't notice much different between them, and of course develop with just activities is much faster. Am i doing the test wrong? Please advice on this scenario.

Or should i try the new Navigation component by Google? They say it will handle the complexity of FragmentTransactions for you.

veeyikpong
  • 829
  • 8
  • 20

1 Answers1

1

I would suggest you to use different activities for different functionalities. From your example of booking flow --> select ticket, select seat, select product details, then enter personal details

Each step is a world of its own. For example, in select seats, you might be showing a seat layout, pricing of each seat and even images. There is a flow of data in each step of the process. And each of those steps might evolve over time to have many functionalities. This makes perfect case for using one activity for each step.

If you had used a single activity and fragments on the back stack, over a period of time it would become very very complicated to manage the life cycle and the flow of data.

But consider a login functionality with a user and password button. There is a possibility of using this feature in many places. Ex. The user can login from a Login Screen or he can login while doing the payment or even when the user is entering his details. This makes perfect case for a fragment(Code once, and use in many places).

Hope this makes sense.:)

Dinesh
  • 948
  • 6
  • 11
  • I understand his scenario, so use fragment only when you need to use it multiple flows in different screens if calling the login activity ( or Any) from different places is not a good idea. – Vikas Pandey Dec 13 '19 at 12:09