gorgi93
gorgi93

Reputation: 2535

How is activity data handled on recreation of activity if it is kept in static class

There is one thing about Android activity lifecycle that I don't understand. When the activity is recreated (for example when I rotate the screen) all instances of it are destroyed so I have to take care to store the data I need to keep. But what happens If I have a static class which is populated with data, and is accessed from this Activity. When the activity gets destroyed will this data be lost or not? Since the class is static there is no need to instantiate it.

Upvotes: 3

Views: 56

Answers (1)

Chris
Chris

Reputation: 3338

Indeed, the static data class will not be recreated nor destroyed. What you should absolutely avoid, is declaring your data in an inner static class in your activity. This will cause a memoryleak because the static class will keep reference to your activity, so the garbage collector will not be able to clean up the old activity in memory, wich will cause your heap to grow each time you rotate your application. But if your static class is outside your activity, it should be no problem!

Here you will find a valuable video on memory managment in android. Even if it's a bit outdated, it will help you understand how the garbage collector works in Android!

Upvotes: 1

Related Questions