O'Reilly logo

Cardboard VR Projects for Android by Matt Schoen, Jonathan Linowes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using the trigger to pick and launch the app

The final piece is to detect which shortcut the user is gazing at and respond to a trigger (click) by launching the app.

When we launch a new app from this one, we need to reference the MainActivity object. One way to do it is to make it a singleton object. Let's do that now. Note that you can get into trouble defining activities as singletons. Android can launch multiple instances of a single Activity class, but even across apps, static variables are shared.

At the top of the MainActivity class, add an instance variable:

    public static MainActivity instance;

Initialize it in onCreate:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;

Now in MainActivity ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required