-
-
Notifications
You must be signed in to change notification settings - Fork 297
Lifecycle of a Processing sketch
This page is intended to give an overview of how a Processing for Android sketch is initialized and run by the Android system, and to help understanding what classes from the core library are involved in handling the low-level rendering and input operations through the Android API. This lifecyle is different depending if the sketch is run as a regular app, a live wallpaper, a watch face, or a VR app. Let's start looking into the details for a regular app:
Every time you run a sketch from from the Android mode as a regular app, for example something as simple as the following code:
void setup() {
fullScreen(P2D);
}
void draw() {
if (mousePressed) {
ellipse(mouseX, mouseY, 50, 50);
}
}
Processing will automatically convert this code into a valid Android project, which will then build into an app package (.apk) using Gradle, and finally install on the device or in the emulator using the Android Debug Bridge (adb) tool available in the Android SDK.
For the code above, Processing will generate two separate java files, one holding the sketch itself and another the main activity that will launch the sketch inside a fragment that contains nothing but our sketch:
package core_debug;
import android.os.Bundle;
import android.content.Intent;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.support.v7.app.AppCompatActivity;
import processing.android.PFragment;
import processing.android.CompatUtils;
import processing.core.PApplet;
public class MainActivity extends AppCompatActivity {
private PApplet sketch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
frame.setId(CompatUtils.getUniqueViewId());
setContentView(frame, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
sketch = new Sketch();
PFragment fragment = new PFragment(sketch);
fragment.setView(frame, this);
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (sketch != null) {
sketch.onRequestPermissionsResult(
requestCode, permissions, grantResults);
}
}
@Override
public void onNewIntent(Intent intent) {
if (sketch != null) {
sketch.onNewIntent(intent);
}
}
}
package core_debug;
import processing.core.PApplet;
public class Sketch extends PApplet {
public void settings() {
fullScreen(P2D);
}
public void draw() {
if (mousePressed) {
ellipse(mouseX, mouseY, 50, 50);
}
}
}