-
Notifications
You must be signed in to change notification settings - Fork 59
Fragment Lifecycle
Fragment lifecycle is always hard. Moreover, If you create multiple tabs with multiple fragments, you need to struggle to show, hiding, attaching, detaching, destroying fragments. So more you struggle with fragment manager, more you get bugs and crash.
Before deep dive into how Medusa makes lifecycle simple for you, It needs to be understood how to attach/detach and show/hide works.
When you detach the fragment, it is removed from the UI, however its state is still being actively managed by the fragment manager. When going into this state its view hierarchy is destroyed.
onPause()
onStop()
methods are called when you detach fragment. As you see onDestroy() is not called. Detaching fragment is good for your memory. Because of UI is destroyed, you can attach more fragments without getting out of memory exception.
Good thing is that, when you attach the fragment which is detached, the fragment will not be created again, its UI state will be loaded back to memory.
onStart()
onResume()
methods are called when you attach the fragment. You will notice that onCreate() method is not called again. Because detached fragment's states are hold in fragment manager. So it will not be created again.
If you want to create performant application better you use attach/detach fragments. And also, LiveData is a great library to make your application lifecycle aware. So If you choose to attach/detach your fragments, you will be friend with lifecycle so with LiveData
of Architecture Components.