Skip to content

Instantly share code, notes, and snippets.

@GregKluska
Created June 14, 2020 13:48
Show Gist options
  • Select an option

  • Save GregKluska/2cf9da39f4f3aee5684363d58cec303b to your computer and use it in GitHub Desktop.

Select an option

Save GregKluska/2cf9da39f4f3aee5684363d58cec303b to your computer and use it in GitHub Desktop.
Check if the content implements an interface
import android.content.Context
import androidx.fragment.app.Fragment
import com.codingwithmitch.mviexample.ui.DataStateListener
import java.lang.ClassCastException
class ExampleFragment : Fragment() {
lateinit var dataStateHandler: DataStateListener
override fun onAttach(context: Context) {
super.onAttach(context)
try {
dataStateHandler = context as DataStateListener
}catch(e: ClassCastException) {
println("DEBUG: $context must implement DataStateListener")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment