问题描述
我有一个主从布局,我有一个活动和5个片段,我使用了一个名为selectedService
的Integer mutablelivedata(我将其保留在共享首选项中),该整数在ServiceAdapter
中设置为点击的服务的ID,并在活动中对其进行观察,以为所选服务打开正确的相应片段
这是我的共享首选项类Settings.kt
:
object Settings {
private const val NAME = "MyPreferences"
private const val MODE = Context.MODE_PRIVATE
// Keys
private const val LANGUAGE_ID_KEY = "language_id"
private lateinit var preferences: SharedPreferences
fun init(context: Context) {
preferences = context.getSharedPreferences(NAME,MODE)
mappingServicesWithFragments()
}
private inline fun SharedPreferences.edit(operation: (SharedPreferences.Editor) -> Unit) {
val editor = edit()
operation(editor)
editor.apply()
}
var languageID: Int
get() = preferences.getInt(LANGUAGE_ID_KEY,1)
set(value) = preferences.edit { it.putInt(LANGUAGE_ID_KEY,value) }
// to handle service clicks and opening the correct fragments associated with those services
var selectedService: mutablelivedata<Int> = mutablelivedata()
private val serviceFragmentMap = HashMap<Int,Fragment>()
private fun mappingServicesWithFragments() {
serviceFragmentMap[1] = InpatientFragment()
serviceFragmentMap[2] = OutpatientFragment()
serviceFragmentMap[3] = ConsultationFragment()
serviceFragmentMap[4] = ReleasedPatientsFragment()
serviceFragmentMap[5] = FavoritesListsFragment()
serviceFragmentMap[6] = PatientProfileFragment()
serviceFragmentMap[7] = VitalSignsFragment()
serviceFragmentMap[8] = DiagnosisFragment()
serviceFragmentMap[9] = NurseNotesFragment()
serviceFragmentMap[10] = RadiologyFragment()
serviceFragmentMap[11] = LaboratoryFragment()
serviceFragmentMap[12] = MedicationsFragment()
serviceFragmentMap[13] = ProceduresFragment()
serviceFragmentMap[14] = OperationsFragment()
serviceFragmentMap[15] = PatientConsultationsFragment()
}
fun getMappedFragment(key: Int): Fragment? {
return serviceFragmentMap[key]
}
这是我在selectedService
上观察到的主要活动:
class MainDoctorActivity : BaseActivity() {
private lateinit var services: List<Service>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_doctor)
initialViews()
}
private fun initialViews() {
// Static List of services for the (Doctor)
services = listof(
Service(1,"Inpatient",R.drawable.ic_inpatients,true),Service(2,"Outpatient",R.drawable.ic_outpatients,false),Service(3,"Consultation",R.drawable.ic_consultation,Service(4,"Released patients",R.drawable.ic_released_patients,Service(5,"Favorites lists",R.drawable.ic_favorites_lists,false)
)
// Setting up the Name and ID of the doctor in main screen
doctorNameTv.text = Settings.loggedindoctor!!.doctorName
doctorIdTv.text = Settings.loggedindoctor!!.doctorID
rvServices.apply {
adapter = ServicesAdapter(services)
addItemdecoration(
DividerItemdecoration(
this@MainDoctorActivity,linearlayoutmanager.VERTICAL
)
)
}
// Observe selectedService
Settings.selectedService.observe(this@MainDoctorActivity,{
openFragment(supportFragmentManager,Settings.getMappedFragment(it))
})
}
fun logout(view: View) {
Utils.animateClickingButton(view)
Settings.loggedindoctor = null
val intent = Intent(this@MainDoctorActivity,LoginActivity::class.java)
startActivity(intent)
}
override fun onResume() {
super.onResume()
// Open "Inpatient Fragment" as soon as you login/open the app
openFragment(supportFragmentManager,Settings.getMappedFragment(services[0].id))
}
openFragment()
函数是一个扩展函数,我使用它来打开所需的片段,这是它的代码:
internal fun openFragment(manager: FragmentManager,fragment: Fragment?) {
manager.beginTransaction().replace(R.id.container,fragment!!).commit()
}
这是整个布局的图片(出于可视化目的)
这里是ReleasedPatientsFragment.kt
:
class ReleasedPatientsFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_released_patients,container,false)
}
}
您可以说我实际上不做任何事情,只是在我在mainActivity中设置的容器中显示片段,但是每当我在片段之间移动时,我都会漏水,解决问题。
这是CanaryLeak分析:
┬───
│ GC Root: System class
│
├─ android.view.inputmethod.InputMethodManager class
│ Leaking: NO (InputMethodManager↓ is not leaking and a class is never leaking)
│ ↓ static InputMethodManager.sInstance
├─ android.view.inputmethod.InputMethodManager instance
│ Leaking: NO (DecorView↓ is not leaking and InputMethodManager is a singleton)
│ ↓ InputMethodManager.mNextServedView
├─ com.android.internal.policy.DecorView instance
│ Leaking: NO (LinearLayout↓ is not leaking and View attached)
│ mContext instance of com.android.internal.policy.DecorContext,wrapping activity com.example.emr.ui.activities.maindoctor.MainDoctorActivity with mDestroyed = false
│ Parent android.view.ViewRootImpl not a android.view.View
│ View#mParent is set
│ View#mAttachInfo is not null (view attached)
│ View.mWindowAttachCount = 1
│ ↓ DecorView.mContentRoot
├─ android.widget.LinearLayout instance
│ Leaking: NO (MainDoctorActivity↓ is not leaking and View attached)
│ mContext instance of com.example.emr.ui.activities.maindoctor.MainDoctorActivity with mDestroyed = false
│ View.parent com.android.internal.policy.DecorView attached as well
│ View#mParent is set
│ View#mAttachInfo is not null (view attached)
│ View.mWindowAttachCount = 1
│ ↓ LinearLayout.mContext
├─ com.example.emr.ui.activities.maindoctor.MainDoctorActivity instance
│ Leaking: NO (Activity#mDestroyed is false)
│ ↓ MainDoctorActivity.services
│ ~~~~~~~~
├─ java.util.Arrays$ArrayList instance
│ Leaking: UNKNowN
│ ↓ Arrays$ArrayList.a
│ ~
├─ com.example.emr.model.Service[] array
│ Leaking: UNKNowN
│ ↓ Service[].[1]
│ ~~~
├─ com.example.emr.model.Service instance
│ Leaking: UNKNowN
│ ↓ Service.fragment
│ ~~~~~~~~
╰→ com.example.emr.ui.fragments.outpatient.OutpatientFragment instance
Leaking: YES (ObjectWatcher was watching this because com.example.emr.ui.fragments.outpatient.OutpatientFragment received Fragment#onDestroy() callback and Fragment#mFragmentManager is null)
key = ca0075c3-8df2-423f-8adf-48cd230a692f
watchDurationMillis = 8513
retainedDurationMillis = 3512
MetaDATA
Build.VERSION.SDK_INT: 29
Build.MANUFACTURER: Google
LeakCanary version: 2.4
App process name: com.example.emr
Analysis duration: 5271 ms
因此,通过这种分析,我可以说出原因是
Leaking: YES (ObjectWatcher was watching this because
com.example.emr.ui.fragments.outpatient.OutpatientFragment received Fragment#onDestroy() callback and
Fragment#mFragmentManager is null)
但是我不知道如何解决,有人可以解释为什么会有泄漏,并采取步骤确保碎片不会引起泄漏吗?
解决方法
我不熟悉CanaryLeak,但这可能是因为您在serviceFragmentMap
中持有每个片段的实例吗?因此,系统永远不会真正对其进行垃圾回收(即使它们不再显示)。还是只发生在OutpatientFragment
上?
顺便说一句,您可以执行以下操作来创建地图(而不是在函数中进行初始化):
val serviceFragmentMap = mapOf(
1 to InpatientFragment(),2 to OutpatientFragment(),...
)
如果您实际上不想保留实例,而只想传递一个ID并获取正确的片段,则可以改为:
// you don't need the map type really,it can infer it
val serviceFragmentMap = mapOf<Int,Class<out Fragment>(
1 to InpatientFragment::class.java,2 to OutpatientFragment::class.java,...
)
fun getMappedFragment(key: Int): Fragment? {
return serviceFragmentMap[key]?.newInstance()
}
这样,您每次调用它时总会得到一个新的片段,这与框架的工作方式更加接近(例如,如果系统销毁并重新创建了片段,则它不会是您的地图中的实例)
我还建议您熟悉Heap Dumps作为发现内存泄漏的一种方式-比看起来容易!您只是做一些可能导致泄漏的工作,执行垃圾收集来清理所有松散的对象,然后捕获转储。然后,您可以按程序包进行排序,深入研究应用程序的内容,并查看潜伏在其中的每一件事。如果有太多东西(例如特定的片段类型),您可以检查它,看看是什么在保留对它的引用并将其保存在内存中