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
|
|
IS_IOS |
bool
|
|
Functions:
| Name | Description |
|---|---|
set_android_context |
Record the current Android |
set_android_fragment_container |
Record the current |
get_android_context |
Return the current Android |
get_android_fragment_container |
Return the current |
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 |
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 |
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
|
Next steps¶
- Read about the broader runtime in Architecture.