Processing for Android

EN ES

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();
}
                

When an activity is associated to our sketch, we can also retrive its fragment manager and window:


import android.app.FragmentManager;
import android.view.Window;

void setup() {
  FragmentManager manager = getFragmentManager();
  Window window = getWindow();
}