Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory Leak with SwipeRefreshLayout

Tags:

android

I've encountered a memory leak with SwipeRefreshLayout and wondering if there's any way to maybe solve this with a workaround?

In a empty project, with a swipe layout as the root of the main activity, if you swipe down, and minimize the app while it's refreshing, even if you put a stop in onStop or onPause, it will continue to eat CPU/Battery as long as the app is open in the background.

The cpu profiler I attached will just repeatedly do what's in that image forever, and the energy profiler shows an always or almost always constant usage while backgrounded.

Any ideas on what I can do to stop this?

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onStop() { //tried in onPause as well
        swipeRefresh.isRefreshing = false
        swipeRefresh.clearAnimation() //I tried with/without this
        super.onStop()
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/swipeRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

enter image description here

like image 472
Ben987654 Avatar asked Jun 27 '19 18:06

Ben987654


1 Answers

In onPause

swipeRefresh.isEnabled = false 

OnResume

swipeRefresh.isEnabled = true 
like image 113
Ashish singh Avatar answered Oct 13 '22 04:10

Ashish singh