30

Which you think is the best way of doing a wizard like application (user can navigate between screens with a next and back button, and each screen has to save some state data) in Android platform.

I mainly can think in two approaches:

  1. Having one activity+view for each screen and then i make the screen switch by calling each activity. What make this nice is that i can use the system back button as my back handler and i don't have to take care of that myself, aslo each activity will save it's own state.

  2. Having one activity and many views, and what i switch views in each screen change, this helps me re-use more code, but makes saving states a mess.

What do you think? Which is the best way of doing this on Android?

Briareos386
  • 1,927
  • 18
  • 34
Lucas S.
  • 13,391
  • 8
  • 46
  • 46

5 Answers5

17

This library is no longer being developed.

Use Android Navigation Component with combination of ViewModels to build a wizard flow.


I've developed a lightweight Android library, which is built on top of Android's ViewPager that can be used for creating wizard like activities. Check it out: WizarDroid.

Nimrod Dayan
  • 3,050
  • 4
  • 29
  • 45
  • This is a nice solution, but not quite general. Reliance on the viewpager makes it necessary to have at least one fragment created on either side, which might not be convenient if the contents of the next step aren't determined yet. – Sergo Pasoevi Apr 11 '14 at 08:33
  • can't figure out how to use Wizardroid that depends on support lib 19.x w/ newer support lib versions – m02ph3u5 Nov 07 '14 at 15:07
  • WizarDroid library is no longer being developed. See https://github.com/nimrodda/wizardroid#deprecated – Nimrod Dayan Jul 13 '19 at 08:46
  • @DarkCygnus, the post was edited to say that, after I made that comment. It’s possible to edit answers, so keep that mind. I will delete the original comment, and this one, you can do the same, because they are all now irrelevant. – David d C e Freitas Jul 15 '19 at 11:37
12

I suggest going with 2 as it fits the goal of activities and views. Saving state in this case is easy - if you use the MVC pattern, you can simply have a model object that is passed along to the views. Each view will have portions of the model that it can read/write. No matter where you are, the model should always have the current state. If you get disposed, just save the model. Restore works automatically since you already read from the model when you show each page.

AdamC
  • 16,087
  • 8
  • 51
  • 67
  • 2
    There is a library that enables you to create wizard functionality very easy, check out [WizarDroid](http://wizardroid.codepond.org). It's well documented and on constant development. – Nimrod Dayan Sep 15 '13 at 08:30
  • @CodePond.org That is exactly what I needed. It works great and took me no time to imitate the example. – theblang Jul 28 '14 at 21:25
6

I've gone with the first approach as it seems more natural. Another app uses ViewFlipper for switching views but that's far from anything like wizard.

yanchenko
  • 56,576
  • 33
  • 147
  • 165
2

9 years ago this was obviously a very different kettle of fish - but I think the best way to do this now is with Fragments.

Have a Fragment for each 'page' in the wizard, letting it handle its own lifecycle and state.

Change page from within each Fragment with Fragment.getFragmentManager() - this returns the FragmentManager from the parent Activity, allowing the Fragment to replace itself.

Luke Needham
  • 3,373
  • 1
  • 24
  • 41
1

I think 2 is better. Put each "page" in a view and then just alternate between showing and hiding them. Makes it trivial to do nice transitions. What state are you thinking of maintaining? The only one that doesn't work automatically would be focus and I think you probably want to reset that every time you switch pages. It is also trivial to catch back if you think that is the right behavior for your app.

With 1 you can reuse almost all of your code (just define your own WizardBase class) but I think activities are much slower to launch (and require more memory) than switching between views.

hacken
  • 2,135
  • 11
  • 11