If we have to start Activity B in Activity A for result as follow
We will call startActivityForResult from Activity A we will set some result in
out activity B and return result.
But what if user presses back button on Activity B how should we handle this
case.
I was running LeakCanary and found surprising result that it was always
leaking my Activity B. reason is that it wasn't finishing and staying in
memory even after returning to Activity B with Back Press.
So here is the solution that I found to resolve this. So quick fix is call
method finishActivity with request code that you have passed while calling
activity B.
class ActivityA: AppCompatActivity(){
private val requestCode = 1001
fun startActivityB(){
startActivityForResult(Intent(this,
ActivityB::class.java).apply
{
putExtra("category_id", currentCategory._id)
putExtra("category_color", currentCategory.color)
putExtra("category_name", currentCategory.name)
}, requestCode)
}
override fun onActivityReenter(resultCode: Int, data: Intent?) {
super.onActivityReenter(resultCode, data)
finishActivity(requestCode)
}
}