简体   繁体   中英

Create one fragment and reuse it

I have 2 fragments a and b in which there is a container for fragments, I need to place a separate fragment x in these 2 containers so that the nested fragment x is not recreated when switching between fragments a and b, create a singleton from fragment x. Please tell me how can i implement this

if i was in this situation, i would modify my design such that x falls outside of a and b. this is because it would be more efficient. because every time you switch the fragments, lets say from a to b, a enters paused state and b enters resumed state. change of state would need component to act on their widgets which would consume processing power. if x was outside, then no processing is required because it's not re-rendered. if you still want to implement things this way, then make the data persistent, not the entire fragment . when a fragment is rendered on screen, it's children must also be rendered. there is no other option. you cannot just render a block on screen but expect some of its children to already be rendered on screen before parent has rendered. this is why you'll see onCreate passes a Bundle param. the param contains all data that required retention and that's why things are able to be rendered the same. it's a good idea to separate data from architecture . hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM