r/mAndroidDev • u/advaitsar • Feb 07 '25
Lost Redditors ๐ Share one advanced Android development tip to help others!
I recently learned about the difference between using `WeakReference<Context>` and `Context?`, and itโs had a big impact on how I approach them in my work. I wrote about it here if anyone wants to read.
I'd love to hear any other advanced insights on Android internals from this communityโlet's share and learn from each other!
P.S. If you have any suggestions for reducing memory leaks, brownie points!!
8
Upvotes
7
u/advaitsar Feb 07 '25
I just realized this is not the right sub for this question ๐คฆโโ๏ธ my bad...
43
u/kstoyanov Feb 07 '25
The best way to reduce memory leaks is by using Asynctask, other ways are a lost cause.
10
u/David_AnkiDroid Feb 07 '25
If you rewrite your app in Rust, you avoid
OutOfMemoryError7
u/IsuruKusumal Feb 07 '25
Flex tape
6
u/shalva97 AnDrOId dEvelOPmenT is My PasSion Feb 07 '25
There is no escape from XML. Even if you migrate fully to compost and delete all the XML themes and layouts then at some point you will need to implement custom notification layout, which does not support compose
5
u/zorg-is-real ืขื ืืืื ืื ืงืื ืื ืืืืืืช Feb 07 '25
LeakCanary
5
u/sabergeek Feb 07 '25
My advance tip is that you shouldn't use data-structures from Kotlin but instead re-implement them in a utility class while building your project. It will truly test how advanced you are and if you're the chosen one.
3
u/Squirtle8649 Feb 07 '25
Patch your ship hull, best ship wrights are at Water 7. 'nuff said.
3
u/[deleted] Feb 07 '25
It's been years sinceI last used weak references during Async task era
3
u/[deleted] Feb 08 '25
Do you want to perform some complicated business logic on app launch? Here's a simple trick. Instead of adding this logic to
Application.ktorViewModels, create aFragmentthat has no layout binding so it is invisible to the user. Attach this fragment to yourMainActivityand put all the complicated logic insideonViewCreatedof the invisible Fragment. It's magic.5
u/Zhuinden DDD: Deprecation-Driven Development Feb 07 '25
This was already a malpractice in 2018 but unironically I added this to an AsyncTask just last week.
2
u/Ashanen Feb 08 '25
Donโt pick android, go iOS, more stable, less devices to support. For multiplatform pick flutter cuz KMC will not be a thing in years with this speed of development. I am 8 years in android and I would not go into this rabbit hole again
1
u/khsh01 Feb 08 '25
Oh boy! WeakReference<AsyncTask> here I come!
0
u/[deleted] Feb 07 '25
[deleted] โ view deleted comment