This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val actor = Actor("Will", "Smith", 53) // (1) | |
val forName = actor.forName // (2) | |
val funFact = actor.getFunFact(forName) // (3) | |
println(funFact) | |
} | |
class Actor(val forName: String, val name: String, val age: Int) { | |
fun getFunFact(key: String?): String? = funFacts[key] // (3') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCleared() { | |
repository = null | |
super.onCleared() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
// debugImplementation because LeakCanary should only run in debug builds. | |
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BackgroundLeakPresenter(val view: BackgroundLeakInterface) { | |
private val exceptionHandler = CoroutineExceptionHandler { _, exception -> | |
// Log exception error to server | |
} | |
private val coroutineScope = CoroutineScope(Job() + exceptionHandler) | |
fun doSomeIntensiveWork() { | |
coroutineScope.launch { | |
delay(120000) // simulate intensive work |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BackgroundSolutionPresenter(val view: BackgroundLeakInterface) { | |
private val exceptionHandler = CoroutineExceptionHandler { _, exception -> | |
// Log exception error to server | |
} | |
private val coroutineScope = CoroutineScope(Job() + exceptionHandler) | |
fun doSomeIntensiveWork() { | |
coroutineScope.launch { | |
delay(120000) // simulate intensive work |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DialogUtilLeak private constructor(private val context: Context?) { | |
fun showError(title: String?, message: String?) { | |
if (context == null || (context as Activity).isFinishing) { | |
return | |
} | |
MaterialAlertDialogBuilder(context) | |
.setCancelable(true) | |
.setTitle(title) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
_binding = ActivitySingletonLeakBinding.inflate(layoutInflater) | |
setContentView(binding.root) | |
binding.button.setOnClickListener { | |
coroutineScope.launch(Dispatchers.IO) { | |
delay(500) // simulate API call | |
withContext(Dispatchers.Main) { | |
onError("An error occured") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object DialogUtilSolution { | |
fun showError(context: Context?, title: String?, message: String?) { | |
if (context == null || (context as? Activity?)?.isFinishing == true) { | |
return | |
} | |
MaterialAlertDialogBuilder(context) | |
.setCancelable(true) | |
.setTitle(title) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun onError(errorMessage: String) { | |
/** | |
* Here we pass a copy of the context as a parameter to every functions | |
* of DialogUtilSolution singleton. | |
* Once the function returns, the copy is popped off the stack | |
* -> no memory leak ✅ | |
* Be careful, this code will still leak if the user changes configuration while | |
* the dialog is displayed because the dialog is not tied to the Activity's lifecycle upon | |
* recreation. Please check "solutionfinal" package for complete solution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AndroidApiLeakFragment : Fragment() { | |
private lateinit var localBroadcastReceiver: BroadcastReceiver | |
var wifiStateChanged = false | |
override fun onStart() { | |
super.onStart() | |
registerBroadCastReceiver() | |
} |
OlderNewer