Must I persist Activity data when onStop() fires?

2019-07-16 12:32发布

I find the official documentation regarding the Activity lifecycle unclear. It's littered with words such as should and might which makes me wonder what I must. When it comes to the onStop()-method, the documentation says the following:

Note: Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop() and onRestart() (or even onStart() methods at all.

It's possible? Do I or do I not? Lets say I have some custom components that rely on some standard Java objects defined as private variables in the Activity class. Do I need to persist these objects in the onStop-method? Can I be guaranteed that these variables have the same content when on onRestart() fires?

edit: link to table referred to in answer.

1条回答
做个烂人
2楼-- · 2019-07-16 13:04

For your specific question, you need to consider the killable column in the overview table. Android (pre-Honeycomb) considers your Activity killable after onPause() has been called. So if you need persistence for the next call, and you want to handle that on the Activity level, that's where you save your activity state.

查看更多
登录 后发表回答