在 Android 方向更改时保持 Tango 实例

Holding Tango Instance on Android Orientation Change

在我的 android 应用程序中,我有一个 Google Tango 服务接口的实例,我想保留它 运行,而不是在每次方向更改时重新启动。

构造函数如下所示:

Tango(Context context, java.lang.Runnable runOnTangoReady)

我不能使用 Fragment 保存它的实例,因为这会导致内存泄漏,因为 Tango 实例保留对第一个 Activity 的引用作为上下文。 来自 Google 文档:

Caution: While you can store any object, you should never pass an object that is tied to the Activity, such as a Drawable, an Adapter, a View or any other object that's associated with a Context.

有什么方法可以推荐我在方向更改时保留服务 运行,而不必使用 android:configChanges

为什么 Tango 需要一个 Context,它不是视图,也不是直接在 UI 中绘制?

谢谢!

使用应用程序上下文实例化您的 Tango 实例,从而避免泄漏上下文。

Context.getApplicationContext():

Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.