Office Hours — Today, June 4

Saturday, June 1

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!
4:00 PM
Mark M.
hello, Suleyman!
how can I help you today?
Suleyman O.
So I have a question about fragment state management :D
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
and bottom navigation can only hold so many buttons, so presumably you will not have that many fragments participating in this system
a SparseArray seems like overkill -- that is for thousands of entries, and if you have thousands of fragments... you are going to have issues :-)
beyond that, make sure the fragments are relatively lightweight
both in terms of view-level stuff and in terms of any per-fragment viewmodels
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
that is what FragmentPagerAdapter does for use with a ViewPager
in that case, the problems start to crop up with lots of pages, which is why there is FragmentStatePagerAdapter
you will not have that many pages
(or the bottom nav equivalent of "pages")
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
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!
4:30 PM
Suleyman O.
has left the room
5:00 PM
Mark M.
turned off guest access

Saturday, June 1

 

Office Hours

People in this transcript

  • Mark Murphy
  • Suleyman Orazgulyyev