HDTutorial
EN

Playback starts YouTube’s embedded player (privacy-enhanced mode).

How to Send Data to a New Fragment with a Factory Method - Android Studio Tutorial

66,656 views October 29, 2018 12:14

Watch on YouTube
ChannelCoding in Flow

Description from the creator

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:
gist.github.com/codinginflow/a05784462aa8e2a609…

____________________
💻 Find the BEST programming tutorials on TutHub:
tuthub.io

⭐ Get my MVVM Caching Course now:
codinginflow.com/caching

❗ Subscribe to the channel:
youtube.com/c/codinginflo...

📨 Subscribe to the Coding in Flow newsletter:
codinginflow.com/newsletter

❓ Join our free developer community:
discord.gg/TSnMvmc

📣 Follow Coding in Flow on other sites:
Facebook: facebook.com/codinginflow
Instagram: instagram.com/codinginflow
TikTok: tiktok.com/@codinginflow
Twitter: twitter.com/codinginflow
Github: github.com/codinginflow

💰 Business requests, sponsoring, etc.: [email protected]

About this tutorial

Coding in Flow published this 12:14 video tutorial on October 29, 2018. It has been watched 66,656 times on YouTube.

Duration
12:14
Published
October 29, 2018
Views
66,656
Likes
1,054

Tags

Share

This tutorial in other languages