Skip to content

Utilities

Platform detection and Android context helpers. These are the lowest layer the rest of the package builds on; you usually only need them if you are writing your own native handler or interop code.

Platform detection and shared helpers.

This module is imported early by most other modules, so it avoids importing platform-specific packages at module level. The detection results are cached the first time IS_ANDROID and IS_IOS are read.

The Android variants also expose a small global registry for the current Activity / Context and the page's fragment container view; both are populated by the bundled Android template before any PythonNative code runs.

Attributes:

Name Type Description
IS_ANDROID bool

True when running inside an Android process (either because ANDROID_* env vars are present or because Chaquopy's java module imports successfully).

IS_IOS bool

True when running inside an iOS app bundle (signaled by PN_PLATFORM=ios, sys.platform == "ios", or a Simulator HOME path). Importing rubicon-objc alone is intentionally not enough to trigger this flag.

Functions:

Name Description
set_android_context

Record the current Android Activity/Context.

set_android_fragment_container

Record the current Fragment root container ViewGroup.

get_android_context

Return the current Android Activity/Context.

get_android_fragment_container

Return the current Fragment container ViewGroup.

IS_ANDROID module-attribute

IS_ANDROID: bool = _get_is_android()

True when running inside an Android process.

The flag is computed once at import time, by checking for ANDROID_* environment variables and trying to import Chaquopy's java module.

IS_IOS module-attribute

IS_IOS: bool = _get_is_ios()

True when running inside an iOS app bundle.

The flag is computed once at import time, by checking PN_PLATFORM=ios, sys.platform == "ios", and the iOS Simulator HOME path.

set_android_context

set_android_context(context: Any) -> None

Record the current Android Activity/Context.

Called by the bundled Android template before any view is created; most app code should not call this directly.

Parameters:

Name Type Description Default
context Any

A Java android.content.Context (typically the current Activity).

required

set_android_fragment_container

set_android_fragment_container(container_view: Any) -> None

Record the current Fragment root container ViewGroup.

Parameters:

Name Type Description Default
container_view Any

A Java android.view.ViewGroup that holds the page's view tree.

required

get_android_context

get_android_context() -> Any

Return the current Android Activity/Context.

Returns:

Type Description
Any

The most recently recorded Android context.

Raises:

Type Description
RuntimeError

If called on a non-Android platform, or before the template has registered a context.

get_android_fragment_container

get_android_fragment_container() -> Any

Return the current Fragment container ViewGroup.

Returns:

Type Description
Any

The most recently recorded fragment container view.

Raises:

Type Description
RuntimeError

If called on a non-Android platform, or before PageFragment has registered its container.

Next steps