Processing for Android

Getting state of application

We can obtain a reference to the main activity or context containing the sketch using the getActivity() and getContext() functions:


import android.app.Activity;
import android.content.Context;

void setup() {
  Activity activity = getActivity();
}
                

The activity will be available only for sketches run as regular or VR apps, since wallpapers and watch faces are services that are not associated to an activity. getContext() should return the context of the application or service in any situation.


import android.content.Context;

void setup() {
  Context context = getContext();
}