Fix memory leak in fragments
This commit is contained in:
Dev Jadiya 2025-06-13 16:50:02 +05:30
parent d3ae925567
commit 21353c5c4d
11 changed files with 48 additions and 18 deletions

View file

@ -258,6 +258,12 @@ public class BookmarkListRootFragment extends CommonsDaggerSupportFragment imple
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onDestroy() {
super.onDestroy();

View file

@ -143,6 +143,11 @@ class BookmarkLocationsFragment : DaggerFragment() {
}
}
override fun onDestroyView() {
super.onDestroyView()
binding = null
}
override fun onDestroy() {
super.onDestroy()
// Make sure to null out the binding to avoid memory leaks

View file

@ -67,6 +67,12 @@ public class BookmarkPicturesFragment extends DaggerFragment {
controller.stop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
@Override
public void onDestroy() {
super.onDestroy();

View file

@ -685,14 +685,23 @@ class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.On
override fun onDestroy() {
try {
compositeDisposable.clear()
// Remove child fragment safely
contributionsListFragment?.let {
childFragmentManager.beginTransaction()
.remove(it)
.commitAllowingStateLoss()
}
childFragmentManager.removeOnBackStackChangedListener(this)
locationManager!!.unregisterLocationManager()
locationManager!!.removeLocationListener(this)
super.onDestroy()
locationManager?.unregisterLocationManager()
locationManager?.removeLocationListener(this)
// Nullify locationManager to prevent leaks
locationManager = null
} catch (exception: IllegalArgumentException) {
Timber.e(exception)
} catch (exception: IllegalStateException) {
Timber.e(exception)
} finally {
super.onDestroy()
}
}
@ -757,7 +766,9 @@ class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.On
override fun onDestroyView() {
super.onDestroyView()
presenter!!.onDetachView()
presenter?.onDetachView()
binding = null
contributionsListFragment = null
}
override fun notifyDataSetChanged() {

View file

@ -527,6 +527,11 @@ after opening the app.
quizChecker!!.cleanup()
locationManager!!.unregisterLocationManager()
// Remove ourself from hashmap to prevent memory leaks
try {
locationManager?.unregisterLocationManager()
} catch (e: Exception) {
Timber.e(e, "Error while cleaning up locationManager")
}
locationManager = null
super.onDestroy()
}

View file

@ -791,7 +791,7 @@ class MediaDetailFragment : CommonsDaggerSupportFragment(), CategoryEditHelper.C
}
compositeDisposable.clear()
_binding = null
super.onDestroyView()
}

View file

@ -122,6 +122,10 @@ class ProfileActivity : BaseActivity() {
public override fun onDestroy() {
super.onDestroy()
compositeDisposable.clear()
// Nullify fragment references to avoid memory leaks
if (::achievementsFragment.isInitialized) achievementsFragment.setHasOptionsMenu(false)
if (::leaderboardFragment.isInitialized) leaderboardFragment.setHasOptionsMenu(false)
contributionsFragment = null
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {

View file

@ -882,6 +882,8 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
uploadCategoriesFragment!!.callback = null
}
onBackPressedCallback.remove()
locationManager?.unregisterLocationManager()
UploadMediaPresenter.presenterCallback = null // Clearing reference
}
/**

View file

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Authors:
* Vlad5250
-->
<resources>
<string name="crash_dialog_title">کامَنٕز گوو رُکِتھ</string>
<string name="crash_dialog_text">Oops. کیہہ تام گوو غلط!</string>
<string name="crash_dialog_comment_prompt">ؤنِیوٚ اَسہِ توٚہہِ کیاہ ٲسِیوٚ کران، تہٕ کٕریٚو تہِ اَسہِ سٕتی شیر بذریعہِ برقی خط. یُس مَدَتھ کَرِ اَسہِ اَتھ شہَرنَس منٛز!</string>
<string name="crash_dialog_ok_toast">شُکریہ!</string>
</resources>

View file

@ -381,8 +381,8 @@
<string name="statistics_thanks">Thanks Received</string>
<string name="statistics_featured">Featured Images</string>
<string name="statistics_wikidata_edits">Images via \"Nearby Places\"</string>
<string name="level">Level %d</string>
<string name="profileLevel">%s (Level %s)</string>
<string name="level" formatted="false">Level %d</string>
<string name="profileLevel" formatted="false">%s (Level %s)</string>
<string name="images_uploaded">Images Uploaded</string>
<string name="image_reverts">Images Not Reverted</string>
<string name="images_used_by_wiki">Images Used</string>

View file

@ -13,7 +13,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#Thu Mar 01 15:28:48 IST 2018
org.gradle.jvmargs=-Xmx1536M
org.gradle.jvmargs=-Xmx4096m -Dkotlin.incremental=true
kotlin.daemon.jvmargs=-Xmx4096m
org.gradle.caching=true
android.enableR8.fullMode=false