Getting the context in a BrowseSupportFragment

108
August 13, 2019, at 06:30 AM

I migrated recently from BrowseFragment to BrowseSupportFragment in Kotlin for an Android TV app.

In the onActivityCreated I set some properties which rely on getting the color. To get the colors I use:

ContextCompat.getColor(context, R.color.fastlane_background);

The issue here is that context is nullable and getColor doesn't accept that.

Every time I need the context, do I need to do something like this:

val ctx = context ?: return
ContextCompat.getColor(ctx, R.color.fastlane_background);

Is this the recommended solution, are there better ways?

Answer 1

Use requireContext() to get a non-null Context associated with your Fragment.

Rent Charter Buses Company
READ ALSO
Setting answers dynamically to questions generated from RecyclerView using post method

Setting answers dynamically to questions generated from RecyclerView using post method

I am creating a survey app in a dual pane layoutRight pane displays questions generated from RecyclerView with fields like EditText, RadioButton etc

91
My app fails with Parcel error when Implementing startForResult

My app fails with Parcel error when Implementing startForResult

I've got a Container with multiple fragments

118
Is there a way for webView to permanently save and store caches from a service worker?

Is there a way for webView to permanently save and store caches from a service worker?

I've got a webapp for android, which is using webview to display my webpageMy webpage has a service worker on index

139
How to rotate one circle anti clockwise canvas android

How to rotate one circle anti clockwise canvas android

I can't solve problem and I need helpI create planets around sun to rotate and I need one planet t rotate anti clockwise and my problem begins

120