4

There's one Activity, one Side menu and many Fragments. when I selected menus in side menu, a specific Fragment added into activity and a previous one is removed. In this application, do I have to cache Fragment object? or just recreate new Fragment every time I selected menu? which one is better?

Cinakyn
  • 648
  • 1
  • 7
  • 20

1 Answers1

6
 do I have to cache Fragment object?

Remember that if you cached a Fragment with multiple view within it, it can cost you memory which can lead to OutOfMemoryException that your heap is full and you are allocating memory to another fragment.

or just recreate new Fragment every time I selected menu? which one is better?

If your fragment does not have a lot of View on it then i would cache only 1 or 2 fragment from each side.

Lets say you have 5 fragments ( 1 - 2 - 3 - 4 - 5) and you are currently at the 3rd fragment I would only cache the 2nd and 4th fragment but not the 1st and 5th fragment, but if I changed fragment to 4th then I would cached 3rd and 5th fragment and remove the cached 2nd fragment. Now if you do this then youll save time rendering it again to the screen. But always remember it can cost you memory.

If you want to a memory wise application specially for device with lower heap size then recreate the fragment each time you select a menu.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63