Skip to content

Listeners

Mert Şimşek edited this page Dec 11, 2018 · 4 revisions

OnNavigatorTransactionListener

You can check out the lifecycle page for OnNavigatorTransactionListener. It is explained there.

OnGoBackListener

Going back is also complicated if fragment manager is controlled by your application. But Medusa takes care of this with the following simple method in your Single Activity.

class MainActivity: Activity() {

     override fun onBackPressed() {
         if (multipleStackNavigator!!.canGoBack()) {
             multipleStackNavigator!!.goBack()
         } else {
             super.onBackPressed()
         }
}

We talk about some edge cases with our team. The case that we talked about is, what if fragment needs to do something before going back?

For instance, the fragment has a recyclerview and showing list of products. What if we want to scroll to top instead of going back when user pressed back button? And If user is already on top of the list, he should go back when back button is pressed.

Here is OnGoBackListener comes to help. If you have a condition in the fragment for going back, you can implement this interface in your fragment.

class ProductsFragment : Fragment(), Navigator.OnGoBackListener {

    override fun onGoBack(): Boolean {
         if(shouldScrollToTop()) {
             scrollToTop();
             return false;
         }
         return true;
    }
}
Clone this wiki locally