42
42
import android .opengl .GLSurfaceView ;
43
43
import android .os .Handler ;
44
44
import android .util .AttributeSet ;
45
+ import android .view .View ;
45
46
import android .widget .RelativeLayout ;
46
47
import android .widget .Toast ;
47
48
import androidx .annotation .NonNull ;
@@ -239,11 +240,7 @@ public void startRenderer(int delayMillis) {
239
240
}
240
241
241
242
private void removeGLSurfaceView () {
242
- ((Activity ) getContext ()).runOnUiThread (() -> {
243
- if (glSurfaceView != null ) {
244
- JmeSurfaceView .this .removeView (glSurfaceView );
245
- }
246
- });
243
+ ((Activity ) getContext ()).runOnUiThread (() -> JmeSurfaceView .this .removeView (glSurfaceView ));
247
244
}
248
245
249
246
@ Override
@@ -265,19 +262,34 @@ public void handleError(String errorMsg, Throwable throwable) {
265
262
public void onStateChanged (@ NonNull LifecycleOwner source , @ NonNull Lifecycle .Event event ) {
266
263
switch (event ) {
267
264
case ON_DESTROY :
268
- /*destroy only if the policy flag is enabled*/
269
- if ( destructionPolicy == DestructionPolicy . DESTROY_WHEN_FINISH ) {
270
- legacyApplication . stop (! isGLThreadPaused ());
271
- }
265
+ // activity is off the foreground stack
266
+ // activity is being destructed completely as a result of Activity#finish()
267
+ // this is a killable automata state!
268
+ jmeSurfaceViewLogger . log ( Level . INFO , "Hosting Activity has been destructed." );
272
269
break ;
273
270
case ON_PAUSE :
274
- loseFocus ();
271
+ // activity is still on the foreground stack but not
272
+ // on the topmost level or before transition to stopped/hidden or destroyed state
273
+ // as a result of dispatch to Activity#finish()
274
+ // activity is no longer visible and is out of foreground
275
+ if (((Activity ) getContext ()).isFinishing ()) {
276
+ if (destructionPolicy == DestructionPolicy .DESTROY_WHEN_FINISH ) {
277
+ legacyApplication .stop (!isGLThreadPaused ());
278
+ } else if (destructionPolicy == DestructionPolicy .KEEP_WHEN_FINISH ) {
279
+ jmeSurfaceViewLogger .log (Level .INFO , "Context stops, but game is still running." );
280
+ }
281
+ } else {
282
+ loseFocus ();
283
+ }
275
284
break ;
276
285
case ON_RESUME :
286
+ // activity is back to the topmost of the
287
+ // foreground stack
277
288
gainFocus ();
278
289
break ;
279
290
case ON_STOP :
280
- jmeSurfaceViewLogger .log (Level .INFO , "Context stops, but game is still running" );
291
+ // activity is out off the foreground stack or being destructed by a finishing dispatch
292
+ // this is a killable automata state!
281
293
break ;
282
294
}
283
295
}
@@ -405,11 +417,12 @@ public void loseFocus() {
405
417
@ Override
406
418
public void destroy () {
407
419
/*skip the destroy block if the invoking instance is null*/
408
- if (legacyApplication == null ) {
409
- return ;
420
+ if (glSurfaceView != null ) {
421
+ removeGLSurfaceView ();
422
+ }
423
+ if (legacyApplication != null ) {
424
+ legacyApplication .destroy ();
410
425
}
411
- removeGLSurfaceView ();
412
- legacyApplication .destroy ();
413
426
/*help the Dalvik Garbage collector to destruct the pointers, by making them nullptr*/
414
427
/*context instances*/
415
428
legacyApplication = null ;
@@ -430,10 +443,10 @@ public void destroy() {
430
443
onRendererCompleted = null ;
431
444
onExceptionThrown = null ;
432
445
onLayoutDrawn = null ;
433
- /*nullifying the static memory (pushing zero to registers to prepare for a clean use)*/
434
446
GameState .setLegacyApplication (null );
435
447
GameState .setFirstUpdatePassed (false );
436
- jmeSurfaceViewLogger .log (Level .INFO , "Context and Game have been destructed" );
448
+ JmeAndroidSystem .setView (null );
449
+ jmeSurfaceViewLogger .log (Level .INFO , "Context and Game have been destructed." );
437
450
}
438
451
439
452
@ Override
@@ -516,11 +529,13 @@ public void bindAppStateToActivityLifeCycle(final boolean condition) {
516
529
/*register this Ui Component as an observer to the context of jmeSurfaceView only if this context is a LifeCycleOwner*/
517
530
if (getContext () instanceof LifecycleOwner ) {
518
531
((LifecycleOwner ) getContext ()).getLifecycle ().addObserver (JmeSurfaceView .this );
532
+ jmeSurfaceViewLogger .log (Level .INFO , "Command binding SurfaceView to the Activity Lifecycle." );
519
533
}
520
534
} else {
521
535
/*un-register this Ui Component as an observer to the context of jmeSurfaceView only if this context is a LifeCycleOwner*/
522
536
if (getContext () instanceof LifecycleOwner ) {
523
537
((LifecycleOwner ) getContext ()).getLifecycle ().removeObserver (JmeSurfaceView .this );
538
+ jmeSurfaceViewLogger .log (Level .INFO , "Command removing SurfaceView from the Activity Lifecycle." );
524
539
}
525
540
}
526
541
}
@@ -917,7 +932,7 @@ public void setShowErrorDialog(boolean showErrorDialog) {
917
932
}
918
933
919
934
/**
920
- * Determines whether the app context would be destructed
935
+ * Determines whether the app context would be destructed as a result of dispatching {@link Activity#finish()}
921
936
* with the holder activity context in case of {@link DestructionPolicy#DESTROY_WHEN_FINISH} or be
922
937
* spared for a second use in case of {@link DestructionPolicy#KEEP_WHEN_FINISH}.
923
938
* Default value is : {@link DestructionPolicy#DESTROY_WHEN_FINISH}.
@@ -926,12 +941,14 @@ public void setShowErrorDialog(boolean showErrorDialog) {
926
941
*/
927
942
public enum DestructionPolicy {
928
943
/**
929
- * Finishes the game context with the activity context (ignores the static memory {@link GameState#legacyApplication}).
944
+ * Finishes the game context with the activity context (ignores the static memory {@link GameState#legacyApplication})
945
+ * as a result of dispatching {@link Activity#finish()}.
930
946
*/
931
947
DESTROY_WHEN_FINISH ,
932
948
/**
933
949
* Spares the game context inside a static memory {@link GameState#legacyApplication}
934
- * when the activity context is destroyed, but the app stills in the background.
950
+ * when the activity context is destroyed dispatching {@link Activity#finish()}, but the {@link android.app.Application}
951
+ * stills in the background.
935
952
*/
936
953
KEEP_WHEN_FINISH
937
954
}
0 commit comments