Singleton object becomes null after app is resumed

戏子无情 提交于 2019-11-26 19:09:29

You're getting crashes because you initialize those variables in one Activity, and expect it to be set always, in the other Activity.

But Android doesn't work like that, you can easily end up crashing because after low memory condition happens, only the current Activity gets recreated at first, previous Activity is recreated on back navigation, and all static variables are nulled out (because the process is technically restarted).

Try it out: put your application in background with HOME button, click the TERMINATE button in Android Studio, then re-launch the app from the launcher. You'll experience this phenomenon.

Solution: check for nulls and re-initialize things in a base activity (or in LiveData.onActive).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!