Fragment and constructor dependencies
Image by Jibin Lawrance
Anyone working on Android development for a while would have come across this crash if they’ve written any fragment code
This happens when Android’s Well, how does one end up here?
For the most part, we can have a code like this and get away with a functioning application that is unless our app goes through a process-death or configuration change and Android starts recreating everything.
Looking at the fixesThere are two ways to go about addressing this issue and let’s look at each of them.
1. Fragment FactoryHere we create a We then assign this factory to the parent activity’s (or fragment) Now the Our above code will look like this
We can further tidy things up a bit by creating a lazy extension function.
Now we can change If we were using a DI library in our project then we can further reduce the boilerplate of creating factories and let the library handle that for us using constructor injection. Using Koin library as an example our activity code will look like this
and we let Koin know of our fragment’s dependency by adding a module to it
Detailed implementation for Koin can be found here. Something similar can be achieved using multibindings in Dagger, refer to this blog Well, if everything looks good so far so then why will we require another way of solving the same issue?
The limitation of using
Let’s say we have a In that case, we need to provide bundle arguments to the fragment instead
2. Bundle argumentsThere is nothing new in this, this is how most of us have been using fragments from the get-go.
What we can do though is create some extension functions to make it a little less awful to work with.
We can now create a fragment instance like this
val mainFragment = MainFragment().withArgs { putString(MainFragment.KEY_TEXT, "Now this won't crash") }
and inside the fragment, we can get the argument value like this
Duplication Issue Using
In this function, we try to find the fragment by a All we need to do now is check if the fragment is already added before adding it to the fragment manager and our final code will look like this.
References [1] https://issuetracker.google.com/issues/136898243#comment24 https://zsmb.co/lets-review-pokedex https://proandroiddev.com/android-fragments-fragmentfactory-ceec3cf7c959
Thanks to Jibin Lawrance.
Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
By Carlos Mota It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update.
By
ProAndroidDev -
droidcon News
Tech Showcases, Developer Resources & Partners
EmployerBrandingHeader
jobs.droidcon.com
![]() Latest Android Jobs
Kotlin Weekly
![]() Your weekly dose of Kotlin
ProAndroidDev
![]() Android Tech Blogs, Case Studies and Step-by-Step Coding
Zalando
![]() Meet one of Berlin's top employers
Academy for App Success
![]() Google Play resources tailored for the global droidcon community |
Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.