Skip to content

Fragment Lifecycle

Mert Şimşek edited this page Nov 17, 2018 · 4 revisions

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.

Attach and Detach Fragments

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.

Show and Hide Fragments

When you hide a fragment, non of fragment lifecycle methods are called. It is same with make a view invisible. All states and UI elements are kept in memory. If you have dozens of fragment in your stack, be aware of memory when you are using hide.

But in some cases, It will be good to hide fragment instead of detach. For example If you don't want to remove and load all UI elements, better you use hide/show fragment instead of attach/detach.

Fragment Lifecycle with Medusa

Default Behavior

Medusa's default behavior NavigatorTransaction.ATTACH_DETACH But you can change the default behavior.

Create a configuration for navigator.

val configuration = NavigatorConfiguration(defaultNavigatorTransaction = NavigatorTransaction.SHOW_HIDE)

Pass configuration as a parameter to navigator.

navigator = MultipleStackNavigator(
            fragmentManager,
            R.id.fragmentContainer,
            rootFragmentList,
            navigatorListener,
            navigatorConfiguration = configuration)

Be aware of changing default behaviour to NavigatorTransaction.SHOW_HIDE. With this, non of your fragment lifecycle methods are called (except creating and destroying). Moreover you can get out of memory If you start lots of fragment in your app.

Combine HIDE_SHOW and ATTACH_DETACH

In some cases, you have exception for some fragments. You may want to NavigatorTransaction.SHOW_HIDE only for special fragment(Heavy UI or another case) and NavigatorTransaction.ATTACH_DETACH all others.

Medusa helps you in that case too.

class SpecialFragment: Fragment(), OnNavigatorTransactionListener {

    override fun getNavigatorTransaction(): NavigatorTransaction {
        return NavigatorTransaction.SHOW_HIDE
    }
}
You can’t perform that action at this time.