Skip to content

Instantly share code, notes, and snippets.

@Leviaran

Leviaran/blog.md Secret

Created February 5, 2021 06:44
Show Gist options
  • Save Leviaran/f42a4c6bde6127585a077477e22c0ec5 to your computer and use it in GitHub Desktop.
Save Leviaran/f42a4c6bde6127585a077477e22c0ec5 to your computer and use it in GitHub Desktop.

##Fragment Factory help you create Constructor

Developer right now cannot add constructore in fragment, this advise come from officially google, when we still create constructore our IDE lint will show warning that suggest us to remove it. Constructor contain argument that will be execute in our fragment and we can create some dependecies injection, But we cant do that. Until now, We still face that we cannot add argument constructor when we initialise fragment. Until we meet the Fragment Factory, Fragment Factory will give you manage/control to build fragment and enable constructor.

Take a look this code, we just need to subclass Fragment Factory and we need customize the implementation and no need use Fragment. After that we must tell the Fragment manager to use this class to initialize the fragment.

https://gist.github.com/44821db5d7398cffe190b2096262e4da

Thanks to android community that can add this, basically this feature come and introduce in AndroidDevSummit last week ago. This release has some new overloads to enable you pass generic class into Fragment Factory to manage creating instace of fragment.

https://gist.github.com/5434389b54fdcc83dc34cc115a322b43

If you want try it before stable you can add this into your build.gradle in level app.

https://gist.github.com/37a1c23e0f242a95de1c4f3eda7ae17c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment