From cb0782c87e72225a09887824155c2f99bb520a80 Mon Sep 17 00:00:00 2001 From: Arushi Kesarwani Date: Thu, 19 Aug 2021 10:46:09 -0700 Subject: [PATCH] Adding activity check to enable Dev mode Summary: It is assumed that there will always be an activity associated on enabling the dev support which is not the case. Hence adding that null check. Changelog: [Android][Fixed] - Added null check for activity in onHostResume() Reviewed By: javache Differential Revision: D30411311 fbshipit-source-id: 8936be2df7f16c355693163347d5e1d94c5ce2e1 --- .../facebook/react/ReactInstanceManager.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java index dd1601bd10e592..3792fe60c963da 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java @@ -598,34 +598,41 @@ public void onHostResume(Activity activity) { mCurrentActivity = activity; if (mUseDeveloperSupport) { - // Resume can be called from one of two different states: + // Resume can be called from one of three different states: // a) when activity was paused // b) when activity has just been created + // c) when there is no activity // In case of (a) the activity is attached to window and it is ok to add new views to it or // open dialogs. In case of (b) there is often a slight delay before such a thing happens. // As dev support manager can add views or open dialogs immediately after it gets enabled // (e.g. in the case when JS bundle is being fetched in background) we only want to enable // it once we know for sure the current activity is attached. + // We want to enable the various devsupport tools in case of (c) even without any activity + + if (mCurrentActivity != null) { + // We check if activity is attached to window by checking if decor view is attached + final View decorView = mCurrentActivity.getWindow().getDecorView(); + if (!ViewCompat.isAttachedToWindow(decorView)) { + decorView.addOnAttachStateChangeListener( + new View.OnAttachStateChangeListener() { + @Override + public void onViewAttachedToWindow(View v) { + // we can drop listener now that we know the view is attached + decorView.removeOnAttachStateChangeListener(this); + mDevSupportManager.setDevSupportEnabled(true); + } - // We check if activity is attached to window by checking if decor view is attached - final View decorView = mCurrentActivity.getWindow().getDecorView(); - if (!ViewCompat.isAttachedToWindow(decorView)) { - decorView.addOnAttachStateChangeListener( - new View.OnAttachStateChangeListener() { - @Override - public void onViewAttachedToWindow(View v) { - // we can drop listener now that we know the view is attached - decorView.removeOnAttachStateChangeListener(this); - mDevSupportManager.setDevSupportEnabled(true); - } - - @Override - public void onViewDetachedFromWindow(View v) { - // do nothing - } - }); + @Override + public void onViewDetachedFromWindow(View v) { + // do nothing + } + }); + } else { + // activity is attached to window, we can enable dev support immediately + mDevSupportManager.setDevSupportEnabled(true); + } } else { - // activity is attached to window, we can enable dev support immediately + // there is no activity, we can enable dev support mDevSupportManager.setDevSupportEnabled(true); } }