Nucleus
LifecycleBackground work

Launch at login

Enable, disable, and query launch at login from Kotlin with one API that picks the backend for the current packaging format.

View as Markdown

The autolaunch module enables, disables, and queries launch at login with one API on Windows (MSIX and Win32), macOS 13+, Linux and Flatpak. It respects choices the user made outside your app.

autolaunchmacOSWindowsLinuxGradle plugin: RecommendedWindow runtime: Not neededLogin detection and the MSIX startup task depend on the packaged build.

Add the dependency

build.gradle.kts
dependencies {
    implementation("dev.nucleusframework:nucleus.autolaunch:3.0.0")
}

For MSIX, set appx { addAutoLaunchExtension = true }. Without it, the API returns UNSUPPORTED.

Check and toggle auto-launch

when (AutoLaunch.state()) {
    AutoLaunchState.ENABLED          -> {}
    AutoLaunchState.DISABLED         -> AutoLaunch.enable()
    AutoLaunchState.DISABLED_BY_USER -> AutoLaunch.openSystemSettings()
    AutoLaunchState.UNSUPPORTED      -> { /* macOS < 13, unsupported Linux setup */ }
    else -> {}
}

Do not retry on BLOCKED_BY_USER. After the user turns the app off in Task Manager or System Settings, enable() returns BLOCKED_BY_USER until they turn it back on there. Send them to openSystemSettings().

Detect a login launch

wasStartedAtLogin(args) reports whether login started the process, for example to start hidden. nucleusApplication caches the answer at startup, so later calls can pass an empty array:

LaunchedEffect(Unit) {
    startedAtLogin = AutoLaunch.wasStartedAtLogin(emptyArray())
}

On macOS, a call made before nucleusApplication returns false. It returns false in dev runs.

Notes

  • On MSIX, Task Manager's Startup apps tab refreshes only after the next login. The stored state is already correct.
  • The Linux unit starts after graphical-session.target and gnome-session-x11-services-ready.target. systemd ignores the second target outside GNOME.

API reference

AutoLaunchReturnsNotes
state()AutoLaunchStateCurrent state.
enable() / disable()AutoLaunchResultTurns auto-launch on or off.
openSystemSettings()BooleanOpens Startup apps (Windows) or Login Items (macOS).
wasStartedAtLogin(args)BooleanWhether login started this process.
isEnabled()BooleanENABLED or ENABLED_BY_POLICY.
isUserLocked()BooleanDISABLED_BY_USER.
diagnostic()StringBackend trace for support tickets.
preload()UnitCall off the UI thread to avoid a first-call stall on macOS.
  • AutoLaunchState: ENABLED, DISABLED, DISABLED_BY_USER, DISABLED_BY_POLICY, ENABLED_BY_POLICY (read-only, MSIX Group Policy), UNSUPPORTED.
  • AutoLaunchResult: OK, UNCHANGED, BLOCKED_BY_USER, BLOCKED_BY_POLICY, UNSUPPORTED, ERROR.

AutoLaunchConfig, set before the first AutoLaunch call:

PropertyDefaultAffects
taskIdNucleusApp.startupTaskId, then appIdMSIX <uap5:StartupTask TaskId>
executablePathThe running executableWin32 Run command
autostartArgument"--nucleus-autostart"Login marker; null omits it
registryValueNameNucleusApp.appName, then appIdWin32 Run value name
backgroundReasonnullFlatpak portal prompt

What's next

Was this page helpful?
Edit on GitHub

Last updated on