Skip to content

Feature request: Consider integrating single-instance check #72

Done

Description

@Empty-57

The libraries that can be referred to are:

Activity

  1. added a commit that references this issue on Dec 1, 2026
  2. lijy91 commented on Dec 1, 2026

    @lijy91
    Member

    Implemented cross-platform single-instance locking and second-instance argument forwarding on Application in libnativeapi/nativeapi-core#111 and #74 (available across C++, C ABI, Dart/Flutter, Rust, C#, JS/TS, Python, Go, and Swift):

    • Application.requestSingleInstanceLock(identifier, arguments) — acquires the OS-level lock on the primary instance (returning true), or forwards arguments and the current working directory to the running primary instance and returns false.
    • Application.hasSingleInstanceLock() / Application.releaseSingleInstanceLock() — inspect or release the lock.
    • ApplicationSecondInstanceEvent (arguments, workingDirectory) — delivered on the primary instance's UI thread when a secondary process launches.
      • Windows: per-session named mutex + overlapped named pipe IPC; secondary instances also call AllowSetForegroundWindow(ASFW_ANY) so the primary window can focus itself immediately.
      • macOS & Linux: exclusive flock lock file (kernel-released on crash) + Unix domain socket IPC.

    Flutter usage example

    import 'dart:io';
    import 'package:flutter/widgets.dart';
    import 'package:nativeapi_flutter/nativeapi_flutter.dart';
    
    void main(List<String> args) {
      WidgetsFlutterBinding.ensureInitialized();
    
      if (!Application.instance.requestSingleInstanceLock('com.example.myapp', args)) {
        exit(0);
      }
    
      Application.instance.addListener((event) {
        if (event is ApplicationSecondInstanceEvent) {
          final window = WindowManager.instance.getCurrent();
          if (window != null) {
            if (!window.isVisible) window.show();
            if (window.isMinimized) window.restore();
            window.focus();
          }
          print('Second instance args: ${event.arguments}, cwd: ${event.workingDirectory}');
        }
      });
    
      runApp(const MyApp());
    }

    See the full runnable example at examples/flutter_single_instance_example.

  3. Empty-57 commented on Dec 1, 2026

    @Empty-57
    Author

    @lijy91 Is there an approximate release date for the next stable version?

  4. lijy91 commented on Dec 1, 2026

    @lijy91
    Member

    @Empty-57 0.6.3 has just been released and published to pub.dev (cnativeapi, nativeapi, and nativeapi_flutter ^0.6.3): https://github.com/libnativeapi/nativeapi/releases/tag/v0.6.3

  5. Empty-57 commented on Dec 1, 2026

    @Empty-57
    Author

    @lijy91 是否有计划完善所有方法/类文档

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions