我有一個使用位置管理器(android.location.LocationManager)的片段。 mLocationManager和mLocationListener(android.location.LocationListener)是私有變量來片段化。LocationListener即使在處理得當的情況下也會泄漏
中的onResume()
mLocationListener = new ABCLocationListener();
mLocationManager = (LocationManager) getActivity().getSystemService(
Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 * 1000L, 0F, mLocationListener);
中的onPause
()
mLocationManager.removeUpdates(mLocationListener);
mLocationListener = null;
我正在爲泄漏ABCLocationListener。
logcat D/LeakCanary:| | mListener = [email protected]87568(0x33733270)
我查過了什麼?
1)創建的ABCLocationListener對象的哈希值與0x33733270不同。
2)onResume和onPause方法只被調用一次。在調用removeUpdates和requestLocationUpdates前兩種方法的監聽器的哈希值是相同
此片段的父類型 - 是ViewPager還是Tabs? 還避免每次從onResume()創建新對象。只需創建一次並註冊並取消註冊。 –