From f31ebaaa93d393d1a858d9065103cf7e7a782464 Mon Sep 17 00:00:00 2001 From: Winson Chung Date: Wed, 5 Aug 2020 22:31:22 -0700 Subject: [PATCH] Fix regression in SysUI boot time - If we defer creating the fragment until the view is attached, that seems to throw off the flow of sysui startup (maybe because the actual nav bar fragment isn't set until later). Instead, pull out the attached listener and clear the fragment ref after the view is attached so that we still release the fragment ref. Bug: 162575036 Test: Kill sysui multiple times and check sysui boot times Test: Switch navigation modes several times and verify there's still only one ref to nav bar fragment & view Change-Id: I636837771e8011747de79c7609443ef562804eea --- .../phone/NavigationBarFragment.java | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java index 6297052550c31..8c5e2ceaeadfd 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/NavigationBarFragment.java @@ -375,6 +375,34 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback } }; + private static class NavBarViewAttachedListener implements View.OnAttachStateChangeListener { + private NavigationBarFragment mFragment; + private FragmentListener mListener; + + NavBarViewAttachedListener(NavigationBarFragment fragment, FragmentListener listener) { + mFragment = fragment; + mListener = listener; + } + + @Override + public void onViewAttachedToWindow(View v) { + final FragmentHostManager fragmentHost = FragmentHostManager.get(v); + fragmentHost.getFragmentManager().beginTransaction() + .replace(R.id.navigation_bar_frame, mFragment, TAG) + .commit(); + fragmentHost.addTagListener(TAG, mListener); + mFragment = null; + } + + @Override + public void onViewDetachedFromWindow(View v) { + final FragmentHostManager fragmentHost = FragmentHostManager.get(v); + fragmentHost.removeTagListener(TAG, mListener); + FragmentHostManager.removeAndDestroy(v); + v.removeOnAttachStateChangeListener(this); + } + } + private final DeviceConfig.OnPropertiesChangedListener mOnPropertiesChangedListener = new DeviceConfig.OnPropertiesChangedListener() { @Override @@ -1470,26 +1498,10 @@ public class NavigationBarFragment extends LifecycleFragment implements Callback if (DEBUG) Log.v(TAG, "addNavigationBar: about to add " + navigationBarView); if (navigationBarView == null) return null; - navigationBarView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() { - @Override - public void onViewAttachedToWindow(View v) { - final NavigationBarFragment fragment = - FragmentHostManager.get(v).create(NavigationBarFragment.class); - final FragmentHostManager fragmentHost = FragmentHostManager.get(v); - fragmentHost.getFragmentManager().beginTransaction() - .replace(R.id.navigation_bar_frame, fragment, TAG) - .commit(); - fragmentHost.addTagListener(TAG, listener); - } - - @Override - public void onViewDetachedFromWindow(View v) { - final FragmentHostManager fragmentHost = FragmentHostManager.get(v); - fragmentHost.removeTagListener(TAG, listener); - FragmentHostManager.removeAndDestroy(v); - navigationBarView.removeOnAttachStateChangeListener(this); - } - }); + NavigationBarFragment fragment = FragmentHostManager.get(navigationBarView) + .create(NavigationBarFragment.class); + navigationBarView.addOnAttachStateChangeListener(new NavBarViewAttachedListener(fragment, + listener)); context.getSystemService(WindowManager.class).addView(navigationBarView, lp); return navigationBarView; }