Question

I had an issue where my app kept toasting messages that I had put inside Oncreate() in my main activity which made me think that for some reason my app was restarting due to an exception. After much time I realised that this was happening because I was setting my app to landscape mode. Now that with the orientation change and the activity getting recreated twice, will it double the objects i'm creating and leaving performance issues on my app?

Was it helpful?

Solution

Your Activity will be destroyed, and all it's members will get garbage collected (if there are no other references to them). Therefore, never keep a reference to your Activity outside its own scope, or it will not get garbage collected!

Depending on the type of objects you're creating, you can use onSaveInstanceState and onRestoreInstanceState to save and restore them, obviously. This might speed up things a little (for example, instead of re-reading objects from a database).

Note that this will recreate the objects as well, instead of referring to the same object. It just might be a bit quicker.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top