One Off the Slack: Resources as a Parameter

Colton Idle asked:

Passing Resources into a composable as a parameter? đź‘Ť or đź‘Ž

…I have a composable that is putting three Images next to each other + a piece of text. Image takes a bitmap = imageFromResource(resources, R.drawable.image1)

So, since imageResource() needs a Resources object, we need to get that Resources object from somewhere.

Google’s Adam Powell suggested an ambient:

If that’s just the android framework Resources object I wouldn’t bother over just doing AmbientContext.current.resources where you need it. The benefits you’d usually get from making it a parameter can’t really materialize since both Context and Resources objects in Android are so weird and tied to requirements that the system gives you one and you can’t meaningfully construct your own to alter behavior like other dependencies

That’s the direction that Colton took:

I’m going to give AmbientContext.current.resources a shot.

I was trying to only pass in Resources because I was trying to write a unit test for this simple composable that basically just displays a string + some images, but thought that surfacing the Resources as a param would be best.


Read the original thread in the kotlinlang Slack workspace. Not a member? Join that Slack workspace here!