Make a wizard like application in Android

2019-01-11 02:53发布

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?

5条回答
老娘就宠你
2楼-- · 2019-01-11 03:21

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.

查看更多
我只想做你的唯一
3楼-- · 2019-01-11 03:24

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.

查看更多
在下西门庆
4楼-- · 2019-01-11 03:29

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.

查看更多
孤傲高冷的网名
5楼-- · 2019-01-11 03:39

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.

查看更多
萌系小妹纸
6楼-- · 2019-01-11 03:42

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.

查看更多
登录 后发表回答