有没有办法防止 Activity 在后台时从内存中删除?

Is there a way to prevent an Activity from being removed from memory while it is in the background?

我用 Fragments 中的控件构建了一个媒体播放应用程序。主要 Activity 托管所有这些片段。这些片段保持对主 Activity 的引用,并在控制发生变化时调用主 Activity 中的方法。

现在我需要防止主要 Activity 在后台时被吞噬。实现这一目标的最佳方法是什么?

目前,我的研究表明我需要将几乎所有主要 Activity 代码移动到主要前台服务,并在主要 Activity 中使用转发方法。需要这样做还是有更简单的方法?

What is the best way to make this possible?

不是。

my research makes it look like I will need to move just about all my main Activity code to a main foreground service and have forwarding methods in the main Activity.

您的前台服务将负责播放音乐。您的 UI(activity/fragments、NotificationMediaStyle、应用程序小部件等)将向服务发送命令,告诉它如何更改播放(暂停、跳过曲目) 、改变音量等)。

Is that how it needs to be done

是的,如果您希望在 UI 处于后台时继续播放媒体。

根本就没有


你可以为此使用后台服务 参考此视频 https://youtu.be/3EQWmME-hNA