NimarkoGram Docs
RU
Docs/Lifecycle

Lifecycle

Manage setup, background jobs and cleanup across plugin reloads.

On this pageEnable and disableApp eventsBackground work

Enable and disable

Use on_plugin_load(self) to register menus and subscriptions. A long operation here delays activation. The engine calls on_plugin_unload(self) when it unloads your plugin. Release resources owned by your code there.

Python
from base_plugin import BasePlugin, AppEvent

class SessionLog(BasePlugin):
    def on_plugin_load(self):
        self.log("Plugin enabled")

    def on_app_event(self, event_type):
        if event_type == AppEvent.RESUME:
            self.log("App returned to the foreground")

    def on_plugin_unload(self):
        self.log("Plugin unloaded")

App events

AppEvent.START, RESUME, PAUSE and STOP describe the application. A user can enable a plugin after the app has already started, so put plugin initialization in on_plugin_load. Returning from another screen can trigger another RESUME. Keep menu registration out of that path to avoid duplicates.

Background work

Use client_utils.run_on_queue(fn) for a short background job. Use android_utils.run_on_ui_thread(fn) for Android View updates. Pass the function itself so the queue can call it at the right time.

If you own a long-running loop, check self.stop_event.is_set(). If a library supplies a function to cancel a subscription, register it with self.register_cleanup(unsubscribe) while your plugin is active.