Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 02d3d43d authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Fixing memory leak in QSB widget

> Instead of recreating the whole fragment, only recreating the views
when reinflating
> Binding the fragment in xml instead of in code

This prevents duplicate fragment binding

Bug: 29120662
Change-Id: I25b942f64d68f25e1358f15d8a919daeebdcff9c
parent 795e37cd
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -20,4 +20,11 @@
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/qsb_container"
        android:padding="0dp" />
 No newline at end of file
        android:padding="0dp" >

    <fragment
        android:name="com.android.launcher3.QsbContainerView$QsbFragment"
        android:layout_width="match_parent"
        android:tag="qsb_view"
        android:layout_height="match_parent"/>
</com.android.launcher3.QsbContainerView>
 No newline at end of file
+10 −18
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ package com.android.launcher3;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
@@ -44,8 +43,6 @@ import com.android.launcher3.compat.AppWidgetManagerCompat;
 */
public class QsbContainerView extends FrameLayout {

    private boolean mBound;

    public QsbContainerView(Context context) {
        super(context);
    }
@@ -58,17 +55,6 @@ public class QsbContainerView extends FrameLayout {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        if (!mBound) {
            FragmentManager fm = ((Launcher) getContext()).getFragmentManager();
            fm.beginTransaction().add(R.id.qsb_container, new QsbFragment()).commit();
            mBound = true;
        }
    }

    @Override
    public void setPadding(int left, int top, int right, int bottom) {
        super.setPadding(0, 0, 0, 0);
@@ -103,6 +89,8 @@ public class QsbContainerView extends FrameLayout {
            getContext().registerReceiver(mRebindReceiver, filter);
        }

        private FrameLayout mWrapper;

        @Override
        public View onCreateView(
                LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@@ -110,7 +98,12 @@ public class QsbContainerView extends FrameLayout {
            if (savedInstanceState != null) {
                sSavedWidgetId = savedInstanceState.getInt(QSB_WIDGET_ID, -1);
            }
            mWrapper = new FrameLayout(getContext());
            mWrapper.addView(createQsb(inflater, mWrapper));
            return mWrapper;
        }

        private View createQsb(LayoutInflater inflater, ViewGroup container) {
            Launcher launcher = (Launcher) getActivity();
            mWidgetInfo = getSearchWidgetProvider(launcher);
            if (mWidgetInfo == null) {
@@ -222,10 +215,9 @@ public class QsbContainerView extends FrameLayout {
        }

        private void rebindFragment() {
            if (getActivity() != null) {
                // Recreate the fragment. This will cause the qsb to be inflated again.
                getActivity().getFragmentManager().beginTransaction()
                        .replace(R.id.qsb_container, new QsbFragment()).commit();
            if (mWrapper != null && getActivity() != null) {
                mWrapper.removeAllViews();
                mWrapper.addView(createQsb(getActivity().getLayoutInflater(), mWrapper));
            }
        }