Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to safely finish an Activity in the onResume() method?

My activity's onResume() reads off some "extras" data from the Intent that started it and updates the UI accordingly.

I'd like to add error handling: if the data in the Intent is missing/corrupted, the activity displays a Toast and finishes.

Can I simply call finish() in the onResume() method? I'm worried about some unexpected things given that both are related to the life cycle.

If there are other better ways, I'm interested in these too, but the above seems simplest.

Thanks!

like image 641
Jan Żankowski Avatar asked May 10 '11 15:05

Jan Żankowski


2 Answers

It is safe for an Activity to self-terminate by calling finish() at any time without it having any detrimental effect.

Obviously you have to be sure you have saved any required settings/data before calling finish() but that goes without saying and is entirely your responsibility based on your Activity design.

like image 179
Squonk Avatar answered Sep 20 '22 07:09

Squonk


Calling finish() in onResume() should be fine. But why do you do the error handling in onResume() and not in onCreate()?

like image 23
Flo Avatar answered Sep 20 '22 07:09

Flo