Skip to content

Commit

Permalink
Adding activity check to enable Dev mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arushikesarwani94 authored and facebook-github-bot committed Aug 19, 2021
1 parent f085e09 commit cb0782c
Showing 1 changed file with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit cb0782c

Please sign in to comment.