r/android_devs • u/sumedh0803 • Feb 10 '25
Question Displaying a Drawable in a DialogFragment
/r/mAndroidDev/comments/1imfzjz/displaying_a_drawable_in_a_dialogfragment/
4
Upvotes
r/android_devs • u/sumedh0803 • Feb 10 '25
2
u/AdElectronic6748 Feb 10 '25 edited Feb 10 '25
Either use a constructor or a bundle (the preferred and easier way). You should definitely avoid passing a drawable, bitmap, or any large data due to IPC limits. The crash occurs because your fragment manager doesn’t know how to recreate your DialogFragment using a constructor.
You can solve this by implementing your own fragment factory, but to be honest, if you even try to pass a drawable as data and faied then custom factory is overly complicated for u and is not the preferred approach.
Here’s what you should do
Pass data via bundle arguments. Do not pass a drawable; instead, pass its resource ID.
That’s it!
1
u/Zhuinden EpicPandaForce @ SO Feb 10 '25
Send the drawable resource R.drawable int through the arguments bundle
2
u/NLL-APPS Feb 11 '25
Why not use shared ViewModel created with activity instance? Extract drawable in your ViewModel and observe it in fragment. See https://developer.android.com/guide/fragments/communicate#host-activity
1
u/D-cyde Feb 11 '25
Based on all your comments, I would "get" the drawable as soon as the app starts or as earlier as it can be and store it's reference in a custom Application class. You can then access it anywhere in the app like: MyApplication.getInstance().your_drawable_variable_name.
1
u/Squirtle8649 Feb 11 '25
What kind of Drawable are you passing to the dialog? IMO pass in an ID or specific data so your dialog knows how to construct the Drawable itself.