Skip to content

Commit 45b87f0

Browse files
committed
revised dispose/exit
1 parent bb24833 commit 45b87f0

File tree

11 files changed

+81
-29
lines changed

11 files changed

+81
-29
lines changed

core/src/processing/android/AppComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,6 @@ abstract public interface AppComponent extends PConstants {
4242
public void requestDraw();
4343
public boolean canDraw();
4444

45+
public void dispose();
4546
public void onPermissionsGranted();
4647
}

core/src/processing/android/PFragment.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public void setSketch(PApplet sketch) {
9494
this.sketch = sketch;
9595
}
9696

97+
public void dispose() {
98+
sketch = null;
99+
}
100+
97101
@Override
98102
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
99103
if (sketch != null) {
@@ -121,8 +125,8 @@ public void onPause() {
121125

122126
@Override
123127
public void onDestroy() {
124-
sketch.onDestroy();
125128
super.onDestroy();
129+
sketch.onDestroy();
126130
}
127131

128132

@@ -147,9 +151,9 @@ public void onConfigurationChanged(Configuration newConfig) {
147151
}
148152

149153

150-
public void onBackPressed() {
151-
sketch.exit();
152-
}
154+
// public void onBackPressed() {
155+
// sketch.exit();
156+
// }
153157

154158

155159
public void onPermissionsGranted() {

core/src/processing/android/PWallpaper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public boolean canDraw() {
100100
return true;
101101
}
102102

103+
public void dispose() {
104+
engine = null;
105+
}
106+
103107
public void onPermissionsGranted() {
104108

105109
}

core/src/processing/android/PWatchFaceCanvas.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public void setSketch(PApplet sketch) {
9393
this.sketch = sketch;
9494
}
9595

96+
public void dispose() {
97+
sketch = null;
98+
engine = null;
99+
}
100+
96101
public void onPermissionsGranted() {
97102

98103
}
@@ -266,7 +271,7 @@ public void onDestroy() {
266271

267272
@Override
268273
public void onDestroy() {
269-
sketch.onDestroy();
270274
super.onDestroy();
275+
sketch.onDestroy();
271276
}
272277
}

core/src/processing/android/PWatchFaceGLES.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public void setSketch(PApplet sketch) {
9090
this.sketch = sketch;
9191
}
9292

93+
public void dispose() {
94+
sketch = null;
95+
engine = null;
96+
}
97+
9398
public void onPermissionsGranted() {
9499

95100
}
@@ -282,7 +287,7 @@ public void onDestroy() {
282287

283288
@Override
284289
public void onDestroy() {
285-
sketch.onDestroy();
286290
super.onDestroy();
291+
sketch.onDestroy();
287292
}
288293
}

core/src/processing/core/PApplet.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,14 +2823,14 @@ public void die(String what, Exception e) {
28232823
die(what);
28242824
}
28252825

2826+
/*
2827+
// No need for these two, as explicit app exit is not required on Android, read
2828+
// this discussion:
2829+
// http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238
28262830
2827-
/**
2828-
* Call to safely exit the sketch when finished. For instance,
2829-
* to render a single frame, save it, and quit.
2830-
*/
2831+
// Call to safely exit the sketch when finished. For instance,
2832+
// to render a single frame, save it, and quit.
28312833
public void exit() {
2832-
// println("exit() called");
2833-
// if (thread == null) {
28342834
if (surface.isStopped()) {
28352835
// exit immediately, stop() has already been called,
28362836
// meaning that the main thread has long since exited
@@ -2853,14 +2853,14 @@ public void exit() {
28532853
}
28542854
}
28552855
2856-
28572856
public void exitActual() {
28582857
try {
28592858
System.exit(0);
28602859
} catch (SecurityException e) {
28612860
// don't care about applet security exceptions
28622861
}
28632862
}
2863+
*/
28642864

28652865
/**
28662866
* Called to dispose of resources and shut down the sketch.

core/src/processing/core/PGraphics.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ protected void allocate() { }
721721
* endRaw(), in order to shut things off.
722722
*/
723723
public void dispose() { // ignore
724+
parent = null;
724725
}
725726

726727

core/src/processing/core/PSurface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,7 @@ public interface PSurface {
7878

7979
public boolean isStopped();
8080

81+
public void finish();
82+
8183
public void setFrameRate(float fps);
8284
}

core/src/processing/core/PSurfaceNone.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,33 @@ public String getName() {
105105
public void dispose() {
106106
sketch = null;
107107
graphics = null;
108-
component = null;
109-
activity = null;
110-
view = null;
108+
109+
if (activity != null) {
110+
// In API level 21 you can do
111+
// activity.releaseInstance();
112+
// to ask the app to free up its memory.
113+
// https://developer.android.com/reference/android/app/Activity.html#releaseInstance()
114+
// but seems redundant to call it here, since dispose() is triggered by
115+
// the onDestroy() handler, which means that the app is already
116+
// being destroyed.
117+
118+
activity = null;
119+
}
120+
121+
if (view != null) {
122+
view.destroyDrawingCache();
123+
view = null;
124+
}
125+
126+
if (component != null) {
127+
component.dispose();
128+
component = null;
129+
}
130+
131+
if (surface != null) {
132+
surface.getHolder().getSurface().release();
133+
surface = null;
134+
}
111135
}
112136

113137
@Override
@@ -222,6 +246,17 @@ public void setSystemUiVisibility(int visibility) {
222246
}
223247
}
224248

249+
@Override
250+
public void finish() {
251+
if (component.getKind() == AppComponent.FRAGMENT) {
252+
activity.finish();
253+
} else if (component.getKind() == AppComponent.WALLPAPER) {
254+
wallpaper.stopSelf();
255+
} else if (component.getKind() == AppComponent.WATCHFACE) {
256+
watchface.stopSelf();
257+
}
258+
}
259+
225260
///////////////////////////////////////////////////////////
226261

227262
// Thread handling
@@ -356,14 +391,7 @@ public void run() { // not good to make this synchronized, locks things up
356391
beforeTime = System.nanoTime();
357392
}
358393

359-
if (sketch != null) {
360-
sketch.dispose(); // call to shutdown libs?
361-
// If the user called the exit() function, the window should close,
362-
// rather than the sketch just halting.
363-
if (sketch.exitCalled) {
364-
sketch.exitActual();
365-
}
366-
}
394+
finish();
367395
}
368396
}
369397
}

core/src/processing/opengl/PGL.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,8 @@ public PGL(PGraphicsOpenGL pg) {
409409

410410
public void dispose() {
411411
destroyFBOLayer();
412+
graphics = null;
413+
sketch = null;
412414
}
413415

414416

src/processing/mode/android/AndroidBuild.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,11 +1494,11 @@ private void writeFragmentActivity(final File srcDirectory, String[] permissions
14941494
writer.println(" fragment.setSketch(sketch);");
14951495
writer.println(" }");
14961496
writer.println(" }");
1497-
writer.println(" @Override");
1498-
writer.println(" public void onBackPressed() {");
1499-
writer.println(" fragment.onBackPressed();");
1500-
writer.println(" super.onBackPressed();");
1501-
writer.println(" }");
1497+
// writer.println(" @Override");
1498+
// writer.println(" public void onBackPressed() {");
1499+
// writer.println(" fragment.onBackPressed();");
1500+
// writer.println(" super.onBackPressed();");
1501+
// writer.println(" }");
15021502

15031503
// Requesting permissions from user when the app resumes.
15041504
// Nice example on how to handle user response

0 commit comments

Comments
 (0)