Mark M. |
has entered the room |
Jun 4 |
3:55 PM |
Mark M. |
turned on guest access |
Suleyman O. |
has entered the room |
Suleyman O. |
Hi Mark!
|
Jun 4 |
4:00 PM |
Mark M. |
hello, Suleyman!
|
Mark M. |
how can I help you today?
|
Suleyman O. |
So I have a question about fragment state management :D
|
Suleyman O. |
View paste
I'm facing a problem with fragment state management while using BottomNavigationView. Initially, I was using replace method to swap between the fragments, which resulted in a little bit of latency and redundant fragment re-creation (since I'm using livedata I don't need to reload the fragments).
Right now, first I add all the fragments in one transaction in the onCreate method, also I add them to a SparseArray to then be able to hide/show fragments outside of onCreate.
Could you tell me if this is the best approach and how can I optimise it?
|
Mark M. |
I can't say if it is the best, but for a small number of fragments, hide/show is not unreasonable
|
Mark M. |
and bottom navigation can only hold so many buttons, so presumably you will not have that many fragments participating in this system
|
Mark M. |
a SparseArray seems like overkill -- that is for thousands of entries, and if you have thousands of fragments... you are going to have issues :-)
|
Mark M. |
beyond that, make sure the fragments are relatively lightweight
|
Mark M. |
both in terms of view-level stuff and in terms of any per-fragment viewmodels
|
Jun 4 |
4:05 PM |
Suleyman O. |
Yep, only 4, I guess I went overboard with Sparse Array, will probably use an array. As for the fragments, they are quite lightweight I think, but I believe there would be a noticeable spike in memory if they weren't?
|
Mark M. |
if you mean in terms of things like the Android Studio memory profiler, yes
|
Suleyman O. |
View paste
yep, exactly. I will make sure to monitor it closely. But would you have any concerns in storing the fragment instances like that in an array?
|
Mark M. |
not really
|
Mark M. |
that is what FragmentPagerAdapter does for use with a ViewPager
|
Mark M. |
in that case, the problems start to crop up with lots of pages, which is why there is FragmentStatePagerAdapter
|
Mark M. |
you will not have that many pages
|
Mark M. |
(or the bottom nav equivalent of "pages")
|
Jun 4 |
4:10 PM |
Suleyman O. |
Yep, that is true, I tried implementing it using a ViewPager and disabling the swipe gesture, but it didn't sit well with my SharedElementTransitions :D
|
Jun 4 |
4:10 PM |
Mark M. |
so, overall, your approach should be fine
|
Suleyman O. |
Perfect, thank you very much Mark, that was very helpful, I needed some assurance as usual :D
|
Mark M. |
you're welcome!
|
Suleyman O. |
Have a great evening and I'll see you later!
|
Mark M. |
have a pleasant evening!
|
Jun 4 |
4:30 PM |
Suleyman O. |
has left the room |
Jun 4 |
5:00 PM |
Mark M. |
turned off guest access |