How to Send Data to a New Fragment with a Factory Method – Android Studio Tutorial
Since the Android system uses the default no-arg constructor when recreating a fragment, we should not pass values over an overloaded constructor with arguments when we instantiate it. Otherwise we get a warning that says « Avoid non-default constructors in fragments: use a default constructor plus Fragment setArguments(Bundle) instead » or we even get an InstantiationException.
The correct way to instantiate a new fragment and passing values to it, is by creating a bundle, setting arguments on this bundle and then passing it to our fragment. Inside the fragment we then retrieve this bundle and get the values out of it. A clean way to do this is by creating a so called « factory method ».
Example code:
https://gist.github.com/codinginflow/a05784462aa8e2a60961539ecd803110
____________________
💻 Find the BEST programming tutorials on TutHub:
https://tuthub.io
⭐ Get my MVVM Caching Course now:
https://codinginflow.com/caching
❗ Subscribe to the channel:
https://www.youtube.com/c/codinginflo…
📨 Subscribe to the Coding in Flow newsletter:
https://codinginflow.com/newsletter
❓ Join our free developer community:
https://discord.gg/TSnMvmc
📣 Follow Coding in Flow on other sites:
Facebook: https://www.facebook.com/codinginflow
Instagram: https://www.instagram.com/codinginflow
TikTok: https://www.tiktok.com/@codinginflow
Twitter: https://twitter.com/codinginflow
Github: https://github.com/codinginflow
💰 Business requests, sponsoring, etc.: info@codinginflow.com
Views :65957
android studio