I’m struggling to achieve the single instance behavior in my gtk4 app. When I run it subsequently from gnome-shell’s terminal, it always opens a new window and when I tried to count the windows associated with the GtkApplication (before app activation) it returns 0.
It seems you’re operating under the impression that gtk_application_get_windows() will return the list of windows of a running application. That’s not, and has never been, the case.
The way single instances work is:
the first instance will claim a token on a shared global resource when calling g_application_run(); on Linux, this is a name on the session bus associated to the user on a machine; that name is the application id
any other instance launched will call g_application_run(), see that there’s already a running instance, call the Activate D-Bus method on that instance, and will immediately return from the g_application_run()
For more information, see the documentation of the GApplication class.
Ah, I see. All works now, I moved the window counter logic inside the activate callback, so now I have only one window despite invoking the app many times.