I want to implement a screen transition like Activity on Android Fragment, but I can't find a good way.
Specifically, there are a, b, and c activities that transition from a to b, and then back to a with a back key, etc., a retains the View state before transitioning to b (scrollView scroll position, CheckBox On/Off, and so on).I would like to implement this in Fragment.
It then transitions from b to c and from c to a.Clear b,c at this time.This can be achieved with Activity by flagging Intent as FLAG_ACTIVITY_CLEAR_TOP.I want to do the same thing.
When I searched the internet for implementation methods, I couldn't find a suitable method.Therefore, I tried and tried it myself, but the implementation ended up with the following method, but the problem remained.Could you tell me if this is the only way or if there is another good way?If you have a good reference site, you can just tell me about it.Please.
○ Screen transition
Using FragmentTransaction#add(int, Fragment, String), the previous screen seems to be empty, so I will hide it using FragmentTransaction#hide(Fragment).
○ Erase the current screen and return to the previous screen
Use FragmentTransaction#remove (Fragment) to erase the currently displayed screen.If that's all, the last screen will remain hidden, so I'll show it again in show (Fragment).
○ Erase all overlapping screens and display the first screen (implementation of FLAG_ACTIVITY_CLEAR_TOP mentioned above)
Acquire all fragments overlapped by FragmentManager#getFragments() and erase them by FragmentTransaction#remove(Fragment).So I will add the first screen using FragmentTransaction #add(int, Fragment, String).
○Problems
·Fragment using show(), hide() does not have a life cycle
·Fragment tags using FragmentTransaction#add(int, Fragment, String) have to be managed in the collection, so it's troublesome.
How about using replace
and popBackStack
in FragmentManager
?
Screen transitions:
fragmentManager
.beginTransacton()
.replace(R.id.container, fragmentB)
.commit();
When you return, you will be popBackStack
with the default onBackPressed
, so you don't need to explicitly:
fragmentManager
.popBackStack();
For transitions that do not need to be returned, clear the existing backstack first and then process the normal transition.
fragmentManager
.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
© 2024 OneMinuteCode. All rights reserved.