@@ -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}
0 commit comments