preface
Nice to meet you 👋
I’m Flywith24 and this is my 56th original post with the Nuggets.
This is the second article in the Fragment multiple return stack series. This article covers the latest Fragment multiple return stack API and whether the new API still has the problem of Navigation Fragment reconstruction.
Let’s get started
With Ian Lake labeling this May 2018 issue as fixed, Android finally supports multiple return stacks of fragments.
If you are new to the Fragment multiple return stack, you can follow the following steps to get the necessary pre-knowledge:
- Fragment returns the stack preparation section
- Jetpack’s Fragment return stack is a Fragment return stack demo
- Never lose the state of AndroidX SaveState ViewModel-SaveState analysis
- AndroidX Fragment1.2.2 Source code analysis
- Major Refactoring of Fragments – Introduces the new state manager for Fragments
- What is the so-called reconstruction problem of Navigation?
What problem does Fragment solve when it supports multiple return stacks?
Let’s briefly review the problems with Navigation management “flat interface”. Details can be found here.
We used Android Studio’s Bottom Navigation template to quickly create a project
The project consists of an Activity (MainActivity) and three fragments (Home, Dashboard, Notifications), The parent Fragment of each TAB can jump to the corresponding child Fragment:
Each time you click the bottom TAB, the corresponding Fragment will create a new instance. This will cause the state of the current TAB to be lost.
In the above picture, click on the Home TAB to enter the HomeChildFragment, then click on the Dashboard TAB and return, the Fragment corresponding to the Home TAB is restored to its original state! This behavior is completely inconsistent with user expectations.
We adjusted the navigation library version to 2.4.0 (currently in alpha) and the issue was resolved.
The effect is as follows:
Does Fragment support multiple return stacks and not rebuild?
Recently, many users reported that there are still “reconstruction” problems when using the Navigation library version.
We use the new version of Navigation and click on each TAB to see the current fragment instance:
As you can see in the screenshot above, clicking on dashboard and Notifications TAB changes the corresponding fragment instance (clicking on home repeatedly does not rebuild the fragment, which will be discussed later in the source code analysis).
New versions of Navigation will still cause fragments to be rebuilt!
☝ highlight
How does the new version of Navigation restore state?
As we know from the above, the new version of Navigation still has the problem of Fragment reconstruction, so how to restore the reconstructed Fragment to the previous state?
The answer is obvious: Android’s SavedState mechanism. For more on SavedState, go.
SavedInstanceState is not empty every time the Fragment is rebuilt:
Suppose we do the following:
- Click in the initialization state
dashboard
TAB to enterDashboardFragment
And then enterdashboard
Details of the pageDashboardChildFragment
- After clicking on
notifications
TAB to enterNotificationsFragment
- Finally click
dashboard
return
Step 1 Saved State and returned stack status after execution:
Step 2 Saved State and returned stack status when the execution is complete:
Step 2 is executed, the Fragment Manager will call the new API fragmentManager. SaveBackStack (” dashboard “) will return to the stack dashboard this child (let’s call it such) in Saved State
Step 3 Saved State and returned stack status when the execution is complete:
Step 3 is executed, the Fragment Manager will call the new API fragmentManager. SaveBackStack (” notifications “), FragmentManager. RestoreBackStack (” dashboard “) save the notifications return stack, restore the dashboard return stack.
Does that make sense? 😉
Specific implementation logic we will be introduced in the source code, please look forward to.
A bit of advertising (push time)
We are the technical department team of Kuaishou Main station, responsible for the engineering research and development of kuaishou main station and some independent businesses. The team integrates the client side, the server side and the Web side to achieve more flexible and efficient cooperation with the business, and supports the rapid development of kuaishou core business through stable basic platform, efficient business development and cutting-edge technology exploration.
Basic Benefits:
- Full six insurance and one housing fund, weekends off, double pay for overtime
- 16 starting salary + 2000 housing allowance + three meals included
- Comfortable working environment, starbucks, Rosen, Subway inside the park
- 16 inch top with MacBook Pro, daily afternoon tea, unlimited free ice cream coffee milk tea
Basic requirements:
- Undergraduate (Daniel can ignore)
- JD list
Select the position from JD and send the resume and the link of the intended position to my wechat: Flywith24
Commitment:
- Follow up and feedback the interview progress in real time
- Help revise your resume
- Android developers get free access to my Planet of Knowledge
About me
People love to do things that give them positive feedback. If this article is helpful to you, please click 👍. This is very important to me
I am Flywith24. Only through discussion with others can we know whether our own experience is real or not. By communicating with others on wechat, we can make progress together.
- The Denver nuggets
- Small column
- Github
- WeChat: Flywith24