How activity data is processed when an activity is restored if stored in a static class

There is one thing about the 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 of saving the data I need to save. But what happens if I have a static class that is filled with data and is accessible from this activity. When the activity is destroyed, will this data be lost or not? Since the class is static, there is no need to create it.

+3


source to share


1 answer


Indeed, the static data class will not be recreated or destroyed. What you should absolutely avoid is declaring your data in an inner static class in your activity. This will cause memory slowdown because the static class will refer to your activity, so the garbage collector will not be able to clean up the old activity in memory, which will cause your heap to grow with every turn of the application. But if your static class is outside of your activity, this shouldn't be a problem!



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

+1


source







All Articles