I want to fetch some important data in onCreate
of my Application, and store them in the memory.
Can I assume the data I store in memory will be there? If the app get destroyed, can I assume onCreate
will always get triggered again?
CodePudding user response:
From the documentation for onCreate
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
If your application is killed in the background by the system, then it would have to call onCreate
again next time it is launched. As long as you re-initialize your static value in onCreate
each time it should be safe - assuming your application class would hold a reference to it to prevent it from being garbage collected.
You may also find this question useful regarding garbage collecting of static variables.
CodePudding user response:
I don't think that you can depend on that. If you really need to recover the information, you'll need to store it somewhere that survives the destruction of the application, like local storage, or a database.