Skip to main content

Android Context

Many Android APIs require a Context object, which allows access to application-specific resources and classes, as well as calls to hardware APIs.

Using Context in a HybridObject

Unlike in TurboModules, a Hybrid Object does not receive a Context via its constructor, as that would make it non-portable. Instead, Nitro exposes the current ReactApplicationContext via the static NitroModules.applicationContext getter, which you can access in your Hybrid Object if needed:

class HybridClipboard: HybridClipboardSpec() {
private val clipboard: ClipboardManager

init {
val context = NitroModules.applicationContext ?: throw Error("No Context available!")
clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
}
}

Using Context in a HybridView

Unlike in a Hybrid Object, a Hybrid View is platform-specific and always needs a Context - e.g. to create Android Views. Therefore all HybridViews receive Context as a constructor argument:

class HybridCameraView(
private val context: ThemedReactContext
): HybridCameraViewSpec() {
override val view: View = View(context)
}