Java – How do I use activities or fragments in multiscreen workflows?

How do I use activities or fragments in multiscreen workflows?… here is a solution to the problem.

How do I use activities or fragments in multiscreen workflows?

I have a workflow with multiple screens with different question and answer options. So there is a problem on screen 1 where the user makes a choice -> the user clicks the Continue button -> screen 2 opens another question -> the user makes a choice and continues -> screen 3 opens, etc….

But actually, given the good maintainability and clarity, I’m not sure which way is the best way to achieve this behavior. I think they are at least three options:

  1. Each screen has its own activity and layout files, and I pass the selected data through intents.

  2. 1 Activities and fragments are different, each fragment has its own layout. If you press the Continue button, the fragment will be replaced by the next fragment.

  3. 1 Activity and different layout files. It’s just that the layout is replaced, and everything else is handled in the activity.

Actually, I’m already starting to use option 2.) to achieve, but I don’t know if this is “too much”.

Android API guidelines state that you should use Fragments for multi-panel UI and reusability. But actually I don’t want to build a multi-pane UI in this case and wouldn’t reuse fragments. So maybe 3.) Is it the way to go? Or even 1.)?

Solution

I will choose your option #3. The reason is:

  • Compared to fragments or layouts, activities take up some memory. The interface between activities and fragments is a bit awkward, although I’m used to it. I don’t recommend it for your case.
  • I love fragments. However, if all fragments look / feel similar, then why spend time having the computer hide/show or replace them. The reason for fragment is explained in Google pages, such as Building a Flexible UI. I don’t think you need a flexible UI like Google’s Intent.
  • I would suggest an activity with at least one fragment for all questions/answers. You only need to use one layout to change the text/content. If the UI is different, replace the layout with another. Anyway, it’s quite normal that Android apps have so many different layouts.
    Android needs some memory to load the layout, but it’s very fast and efficient.

For option 3 designs:
You can use the inflate() method to load a specific layout and replace one. A good example in SO links @ Android layout replacing a view with another view on run time

Related Problems and Solutions