gpt4 book ai didi

android - SwipeRefreshLayout 的内存泄漏

转载 作者:行者123 更新时间:2023-12-01 19:26:47 26 4
gpt4 key购买 nike

我在使用 SwipeRefreshLayout 时遇到了内存泄漏问题,想知道是否有任何方法可以通过解决方法来解决此问题?

在一个空项目中,以滑动布局作为主要 Activity 的根,如果你向下滑动,并在刷新时最小化应用程序,即使你在 onStop 或 onPause 中停止,它也会继续吃 CPU/只要应用程序在后台打开,电池就会消耗。

我附加的 CPU 分析器将永远重复执行该图像中的操作,并且能量分析器在后台显示始终或几乎始终恒定的使用情况。

有什么想法可以阻止这种情况发生吗?

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

最佳答案

在 onPause 中

swipeRefresh.isEnabled = false 

恢复时

swipeRefresh.isEnabled = true 

关于android - SwipeRefreshLayout 的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56796929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com