Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StartActivityForResult() possible crash?

I have a quick question I hope someone else has a quick answer to: If you start activity [A] and then start activity [B] with startActivityForResult(), is there a chance that when [B] is finished and returning a value(Intent) that [A] could have been GC'd and thus everything crashes?

I read somewhere that this could happen, and thus you shouldn't pass vital information back using Intent.putExtras() because it could be lost? Instead people recommend saving state in sharedPreferences and simply using startActivity() when both going from [A] to [B], and then again when going back from [B] to [A]?

I'd really like to avoid recoding the project I'm on because of this, but if it could be an issue I would obviously like to take care of it before release.

However, if this is possible couldn't you just put a few static references from [A] that you use in [B] and thus making a hard reference, and not allowing GC?

Thanks for any help guys!

like image 962
While-E Avatar asked Mar 15 '26 17:03

While-E


1 Answers

If A has been destroyed, it will be recreated. From the Activity documentation in the Android Dev Guide:

protected void onActivityResult (int requestCode, int resultCode, Intent data)

[..] You will receive this call immediately before onResume() when your activity is re-starting.

That means Activity A will be recreated normally (executing onCreate(), onStart() and then onActivityResult(), as described in the Activity lifecycle) and the result Intent you set in Activity B will be passed.

like image 125
Jacob Ras Avatar answered Mar 17 '26 08:03

Jacob Ras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!