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
, add a handler to the Cardboard trigger:
@Override public void onCardboardTrigger(){ overlayView.onTrigger(); }
Then, in OverlayView
, add the following method:
public void onTrigger() { shortcuts...