Object Sharing Between Activities in Android

为君一笑 提交于 2019-12-04 06:54:14

The main downside to this approach is the fact that the Android system may kill your process at any point in time. Typically this is done when memory is needed for other purposes (like an incoming phone call for example). Android will then re-start your process, and re-create each activity (when the activity becomes visible). In doing so it will call each activity's onCreate method, passing it a bundle of the parcelized data that the activity has saved.

The application class will be re-created as well. The object you have saved there however will not be re-created.

Thus, as painful as it is, parcelization ensures that your object can be resorted to the state it was in when the activity (and the process) was killed.

Another approach would be to persist the object yourself. Using SharedPreferences is one common approach.

====

So in summary, I suggest passing the object from activity to activity via Intent. The intent that is used to launch an activity is always fully re-constituted when that activity is re-created, even in the case of a process kill/re-launch.

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