From bfa5f4c95306fb127c5676153c6e2cd393218eec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Wed, 26 Jul 2017 16:41:27 +0200 Subject: [PATCH] [Android] Fix resource leak when JitsiMeetView is destroyed In iOS this is automagically done in the view destructor, bunt we don't have that luxury in Java we have to do it manually. The new disponse() method MUST be called when the Activity holding the view is going to be destroyed, typically in the onDestroy() handler. --- android/README.md | 9 +++++++++ .../org/jitsi/meet/sdk/JitsiMeetActivity.java | 5 +++++ .../org/jitsi/meet/sdk/JitsiMeetView.java | 20 +++++++++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/android/README.md b/android/README.md index fe2a30049..27eec01fd 100644 --- a/android/README.md +++ b/android/README.md @@ -56,6 +56,9 @@ public class MainActivity extends AppCompatActivity { protected void onDestroy() { super.onDestroy(); + view.dispose(); + view = null; + JitsiMeetView.onHostDestroy(this); } @@ -102,6 +105,12 @@ See JitsiMeetView.setWelcomePageEnabled. The `JitsiMeetView` class is the core of Jitsi Meet SDK. It's designed to display a Jitsi Meet conference (or a welcome page). +#### dispose() + +Releases all resources associated with this view. This method MUST be called +when the Activity holding this view is going to be destroyed, usually in the +`onDestroy()` method. + #### getListener() Returns the `JitsiMeetViewListener` instance attached to the view. diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java index d24f796a2..87cf26a84 100644 --- a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java +++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java @@ -154,6 +154,11 @@ public class JitsiMeetActivity extends AppCompatActivity { protected void onDestroy() { super.onDestroy(); + if (view != null) { + view.dispose(); + view = null; + } + JitsiMeetView.onHostDestroy(this); } diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java index b1a906d9e..35e262ffb 100644 --- a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java +++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java @@ -199,6 +199,21 @@ public class JitsiMeetView extends FrameLayout { views.add(this); } + /** + * Releases the React resources (specifically the {@link ReactRootView}) + * associated with this view. + * + * This method MUST be called when the Activity holding this view is destroyed, typically in the + * {@code onDestroy} method. + */ + public void dispose() { + if (reactRootView != null) { + removeView(reactRootView); + reactRootView.unmountReactApplication(); + reactRootView = null; + } + } + /** * Gets the {@link JitsiMeetViewListener} set on this {@code JitsiMeetView}. * @@ -257,10 +272,7 @@ public class JitsiMeetView extends FrameLayout { // TODO: ReactRootView#setAppProperties is only available on React // Native 0.45, so destroy the current root view and create a new one. - if (reactRootView != null) { - removeView(reactRootView); - reactRootView = null; - } + dispose(); reactRootView = new ReactRootView(getContext()); reactRootView.startReactApplication(reactInstanceManager, "App", props);