Android IllegalStateException while finishing Activity [closed]
Asked Answered
P

1

17

I got this exception

"java.lang.IllegalStateException: Can not be called to deliver a result" 

And didn't immediately understand why. Below is my answer to why this happened, maybe this helps someone.

Puling answered 16/10, 2014 at 21:33 Comment(1)
Well, this has helped me and it looks as if it has helped other as well. So maybe not so off-topic right?Yecies
P
39

I had called finishAffinity() after having set a result code. Android was complaining that it will never be able to deliver the result when calling finishAffinity().

Solution: either don't set the result before calling finishAffinity(), or set the result again before calling finishAffinity() but this time with Activity.RESULT_CANCELED as the result code:

setResult(Activity.RESULT_CANCELED);
finishAffinity();
Puling answered 16/10, 2014 at 21:33 Comment(3)
Not sure if the API changed, but I believe this is supposed to call setResultCode() instead of setResult().Anatol
I'm still seeing setResult() in the docs...? developer.android.com/reference/android/app/…Puling
@DaveLeeds there is no "setResultCode()" for Activities, only BroadcastReceiversEthelynethene

© 2022 - 2024 — McMap. All rights reserved.