简体   繁体   English

碎片 memory 泄漏

[英]Fragments memory leaks

I have only one activity and all navigation is solved through fragments.我只有一个活动,所有导航都是通过片段解决的。 If I open the fragment and come back, the fragment will be still in memory and memory will be growing.如果我打开片段并返回,片段将仍在 memory 中,并且 memory 将不断增长。

What am I doing wrong?我究竟做错了什么? Do I have to manually remove listeners?我必须手动删除侦听器吗? In the onDestroy method, getView is already null.onDestroy方法中, getView已经是null。

How I add fragments:我如何添加片段:

    fun replaceFragment(
        fragmentManager: FragmentManager?,
        fragment: Fragment?,
        frameId: Int,
        tag: String?
    ) {
        if (fragmentManager != null && fragment != null) {
            val previousFragment = fragmentManager.findFragmentById(frameId)
            if (fragment != previousFragment) {
                val transaction = fragmentManager.beginTransaction()
                transaction.replace(frameId, fragment, tag)
                transaction.addToBackStack(tag)
                transaction.commitAllowingStateLoss()
            }
        }
    }

Fragment close by the back button:通过后退按钮关闭片段:

EDIT: How I create Fragment编辑:我如何创建片段

    companion object {
        fun create(number: Long): ManageUnitFragment {
            val fragment = ManageUnitFragment()

            val params = Bundle()
            params.putLong(Keys.Number, number)
            fragment.arguments = params

            return fragment
        }
    }

How I set Listeners (on buttons)我如何设置监听器(在按钮上)

import kotlinx.android.synthetic.main.myFragment.*

        vMyButton.setOnClickListener {
            myAction()
        }

EDIT If I remove this line vButtonClose.setOnClickListener... , the ManageUnitFragments will no longer be in the heap dump.编辑如果我删除此行vButtonClose.setOnClickListener... , ManageUnitFragments 将不再在堆转储中。

ManageUnitFragment:管理单元片段:

import kotlinx.android.synthetic.main.fragment_manage_unit.*

class ManageUnitFragment : Fragment() {

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_manage_unit, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        vButtonClose.setOnClickListener {
            activity?.onBackPressed()
        }
    }

    companion object {
        fun create(unitNumber: Long, wagonWagonUnit: WagonUnit): ManageUnitFragment {
            val fragment = ManageUnitFragment()
            return fragment
        }
    }
}

堆转储

Have you tried to add 'many' fragments?您是否尝试添加“许多”片段? Do you actually get OutOfMemoryException ?你真的得到OutOfMemoryException吗?

Maybe they just remain in memory until more of it is needed.也许他们只是留在 memory 中,直到需要更多。 Then they will be garbage collected.然后它们将被垃圾收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM