Skip to content

Commit b64acb2

Browse files
committed
finish() is not called by the animation thread, but by PApplet.exit()
1 parent d2fe5ba commit b64acb2

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

core/src/processing/a2d/PSurfaceAndroid2D.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ public SurfaceHolder getHolder() {
9999
public void surfaceCreated(SurfaceHolder holder) {
100100
surfaceReady = true;
101101
if (requestedThreadStart) {
102-
// Only start the thread once the surface has been created, otherwise
103-
// it will not be able draw
102+
// Only start the thread once the surface has been created, otherwise it will not be able to draw
104103
startThread();
105104
}
106105
if (PApplet.DEBUG) {

core/src/processing/core/PApplet.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ public void onCreate(Bundle savedInstanceState) {
616616

617617
public void onDestroy() {
618618
handleMethods("onDestroy");
619+
620+
surface.stopThread();
621+
619622
dispose();
620623
}
621624

@@ -2859,22 +2862,14 @@ public void create() {
28592862

28602863
}
28612864

2865+
/**
2866+
* Should trigger a graceful activity/service shutdown (calling onPause/onStop, etc).
2867+
*/
28622868
public void exit() {
2863-
if (getSurface().getComponent().isService()) {
2864-
PGraphics.showWarning("This sketch is running as a live wallpaper or a watch face, and cannot be stopped with exit().\n" +
2865-
"Use the corresponding selector on the device to change the current wallpaper or watch face.");
2866-
} else {
2867-
// This is the correct way to stop the sketch programmatically, according to the developer's docs:
2868-
// https://developer.android.com/reference/android/app/Activity.html#onDestroy()
2869-
// https://developer.android.com/reference/android/app/Activity.html#finish()
2870-
// and online discussions:
2871-
// http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238
2872-
// finish() it will trigger an onDestroy() event, which will translate down through the
2873-
// activity hierarchy and trigger stopping Processing's animation thread, etc.
2874-
getActivity().finish();
2875-
}
2869+
surface.finish();
28762870
}
28772871

2872+
28782873
/**
28792874
* Called to dispose of resources and shut down the sketch.
28802875
* Destroys the thread, dispose the renderer, and notify listeners.

core/src/processing/core/PSurfaceNone.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -362,17 +362,24 @@ public void setSystemUiVisibility(int visibility) {
362362

363363
@Override
364364
public void finish() {
365-
// No need to do anything here, as the Android system will take care of
366-
// releasing activity resources if needed, etc.
367-
// if (component == null) return;
368-
//
369-
// if (component.getKind() == AppComponent.FRAGMENT) {
370-
// activity.finish();
371-
// } else if (component.getKind() == AppComponent.WALLPAPER) {
372-
// wallpaper.stopSelf();
373-
// } else if (component.getKind() == AppComponent.WATCHFACE) {
374-
// watchface.stopSelf();
375-
// }
365+
if (component == null) return;
366+
367+
if (component.getKind() == AppComponent.FRAGMENT) {
368+
// This is the correct way to stop the sketch programmatically, according to the developer's docs:
369+
// https://developer.android.com/reference/android/app/Activity.html#onDestroy()
370+
// https://developer.android.com/reference/android/app/Activity.html#finish()
371+
// and online discussions:
372+
// http://stackoverflow.com/questions/2033914/quitting-an-application-is-that-frowned-upon/2034238
373+
// finish() it will trigger an onDestroy() event, which will translate down through the
374+
// activity hierarchy and eventually pausing and stopping Processing's animation thread, etc.
375+
activity.finish();
376+
} else if (component.getKind() == AppComponent.WALLPAPER) {
377+
// stopSelf() stops a service from within:
378+
// https://developer.android.com/reference/android/app/Service.html#stopSelf()
379+
wallpaper.stopSelf();
380+
} else if (component.getKind() == AppComponent.WATCHFACE) {
381+
watchface.stopSelf();
382+
}
376383
}
377384

378385
///////////////////////////////////////////////////////////
@@ -498,13 +505,11 @@ public void run() { // not good to make this synchronized, locks things up
498505
while ((Thread.currentThread() == thread) &&
499506
(sketch != null && !sketch.finished)) {
500507
if (Thread.currentThread().isInterrupted()) {
501-
finish();
502508
return;
503509
}
504510
try {
505511
checkPause();
506512
} catch (InterruptedException e) {
507-
finish();
508513
return;
509514
}
510515

@@ -540,8 +545,6 @@ public void run() { // not good to make this synchronized, locks things up
540545

541546
beforeTime = System.nanoTime();
542547
}
543-
544-
finish();
545548
}
546549
}
547550

0 commit comments

Comments
 (0)