Switching between Fragments in a single Activity

后端 未结 3 1977
清酒与你
清酒与你 2021-02-03 10:12

I want to create an Activity which shows a sort of menu a user can go through. By clicking an item, a new screen is shown, allowing the user more options (wizard-li

3条回答
  •  时光取名叫无心
    2021-02-03 11:17

    Personally, I would not have any elements.

    Step #1: Populate your activity layout with a for the variable piece of the wizard, plus your various buttons.

    Step #2: In onCreate() of the activity, run a FragmentTransaction to load the first wizard page into the FrameLayout.

    Step #3: On the "next" click, run a FragmentTransaction to replace the contents of the FrameLayout with the next page of the wizard.

    Step #4: Add in the appropriate smarts for disabling the buttons when they are unusable (e.g., back on the first wizard page).

    Also, you will want to think about how the BACK button should work in conjunction with any on-screen "back" button in the wizard. If you want them to both behave identically, you will need to add each transaction to the back stack and pop stuff off the back stack when you handle the "back" button.

    Someday, if nobody beats me to it, I'll try to create a wizard-by-way-of-fragments example, or perhaps a reusable component.

提交回复
热议问题