Search code examples
androidandroid-fragmentsandroid-orientationgoogle-project-tangoorientation-changes

Holding Tango Instance on Android Orientation Change


In my android app, I have an instance of the Google Tango service interface that I would like to keep running, and not restart on every orientation change.

The constructor looks like this:

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

I cannot use a Fragment holding its instance, since this will lead to memory leaks, as the Tango instance keeps a reference to the first Activity as a Context. From the Google Docs:

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.

Is there a way you would recommend me to keep the service running on orientation change, without having to use android:configChanges ?

Why does Tango need a Context, when it is not a View, nor it's drawing directly in UI?

Thank you!


Solution

  • Instantiate your Tango instance with application context, thus refraining yourself from leaking context.

    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.