简体   繁体   English

为什么在单击最小化按钮而不是在“applicationDidEnterBackground”之后单击关闭按钮会调用“applicationWillTerminate”?

[英]Why does clicking the close button call `applicationWillTerminate` after `applicationDidEnterBackground` while clicking the minimize button doesn't?

I create a new empty project in Unity and build it on iOS platform and run it on Mac Catalyst.我在 Unity 中创建了一个新的空项目并在 iOS 平台上构建它并在 Mac Catalyst 上运行它。 However, I want to figure out the difference between close and minimize.但是,我想弄清楚关闭和最小化之间的区别。 With the help of log, they both called applicationWillResignActive and applicationDidEnterBackground .在日志的帮助下,他们都调用了applicationWillResignActiveapplicationDidEnterBackground But there are no backgroundTasks to do, so why does clicking the close button call applicationWillTerminate after applicationDidEnterBackground while clicking the minimize button doesn't?但是没有 backgroundTasks 要做,那么为什么单击关闭按钮会在applicationWillTerminate之后调用applicationDidEnterBackground而单击最小化按钮却没有呢?

And I ask this question because if there are backgroundTasks to do, then clicking close button doesn't really quit the app, it will do backgroundTasks.我问这个问题是因为如果有 backgroundTasks 要做,那么单击关闭按钮并不会真正退出应用程序,它会执行 backgroundTasks。 So I want when clicking close button, I don't want to do backgroundTasks, just call applicationWillTerminate and quit it.所以我想在点击关闭按钮时,我不想做 backgroundTasks,只需调用applicationWillTerminate并退出它。 But How do I do that?但是我该怎么做呢? It confuses me very long time...迷惑了我很久...

Any suggestions will be really appreciate!!任何建议将不胜感激!

You said:你说:

Close and minimize both enter background and do background tasks, is it right?关闭和最小化既进入后台又做后台任务,对吗?

Yes, the beginBackgroundTask tasks will continue, regardless of whether the user minimized the app or closed the window.是的, beginBackgroundTask任务将继续,无论用户是最小化应用程序还是关闭窗口。 It will even do this if the user explicitly quits the app.如果用户明确退出应用程序,它甚至会这样做。 Only crash or force-quitting will not allow background tasks to proceed.只有崩溃或强制退出将不允许后台任务继续进行。

I don't want my app do background tasks after clicking close because background task takes a few seconds to finish.我不希望我的应用程序在单击关闭后执行后台任务,因为后台任务需要几秒钟才能完成。

The problem is that you do not know if it has been minimized or closed.问题是你不知道它是被最小化还是关闭了。 The macOS willMiniaturizeNotification notification is not available to Catalyst apps. macOS willMiniaturizeNotification通知不适用于 Catalyst 应用程序。

The application needs to wait to quit until the background task is finished.应用程序需要等待后台任务完成后退出。 I just want to quit my app immediately after clicking close and don't need to do background tasks, but I don't know how?我只想在点击关闭后立即退出我的应用程序并且不需要执行后台任务,但我不知道如何?

This simply may not be possible in Catalyst apps.这在 Catalyst 应用程序中可能根本不可能。 Given that this is completely transparent to the end user and only takes a few seconds, you may have to decide whether to live with this constraint.鉴于这对最终用户是完全透明的并且只需要几秒钟,您可能必须决定是否接受此限制。 If not, you may be forced to consider AppKit rewrite.如果没有,您可能会被迫考虑重写 AppKit。


The above was a Catalyst question, but for AppKit developers, to get the app to exit when you close the window, implement applicationShouldTerminateAfterLastWindowClosed .以上是一个 Catalyst 问题,但对于 AppKit 开发人员来说,要在关闭窗口时退出applicationShouldTerminateAfterLastWindowClosed ,请实现applicationShouldTerminateAfterLastWindowClosed Not relevant here, but it is how to control the behavior of the close button in AppKit.这里不相关,但它是如何控制 AppKit 中关闭按钮的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM