What I learned
- ViewPager2
- Fragment lifecycle
- fragment.arguments
- Repeated View (UI) initialization
- ZoomOutPageTransformer animation
Key Function
- Conduct MBTI tests through multiple fragment pages on ViewPager.
- View results based on selected categorized data in the result page.
Fragment initializing
- onCreate()
- Retrieve the data from Fragment arguments
- onCreateView()
- Inflate the fragment layout, get view references, set up data on the views, and return the view
- onViewCreated()
- Define the functionality of an interactive UI component like the nextButton
How it works
- Navigate from MainActivity to TestActivity using Intent.
- TestActivity contains a ViewPager2 object and a moveToNextQuestion() method to control it.
- Apply the adapter in TestActivity, passing the instance of TestActivity to ViewPagerAdapter during this process.
- In ViewPagerAdapter, reference TestActivity's member variables, methods, and layout objects using the received context.
- Note that ViewPagerAdapter receives the Activity's instance as FragmentActivity, so a type-cast to TestActivity is required to access the Activity instance's members. Otherwise, only FragmentActivity's members can be accessed.
- ViewPagerAdapter, using TestActivity's context, implements the slide function between fragments created by itself.
- ViewPagerAdapter has members from QuestionFragment to create and return fragments.
- In QuestionFragment, configure the UI, use
(activity as TestActivity)
to store test results data in TestActivity, and callTestActivity.moveToNextQuestion()
to move to the next page.- There is logic in
TestActivity.moveToNextQuestion()
to navigate to the result Activity when reaching the last page.
- There is logic in
- Check the result page, and if the retryButton is clicked, move to MainActivity with the option
Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
.
Something I’m not sure about
- When wanting control over a View component, which approach is preferable? By lazy or lateinit?
- Is it the best approach to manage all Fragment data within an Activity?
What could be better
- Managing UI data using a data class instead of individual processing would enhance maintainability.