Lifetime of a static variable in Android

前端 未结 14 2267

when I declare and initialize a variable as static in my main activity and the activity gets destroyed. Can I still access the content of the variable?

For example t

14条回答
  •  没有蜡笔的小新
    2020-12-03 10:28

    Once I have developed the application using lots of static vars, The problem with static vars is that once android has low memory it automatically removes the static variable and if your views are connected with static variable it won't show or worse application will crash. I recommend using shared preferences or other methods to store the variables.

    problems

    1. If your android memory is low the static variables will be removed from ram and you have to reinitialize them

    2. If your view is attached to the static variables like ArrayList to recycler view it will throw errors like null pointer exceptions for that you have to check balance every time you initialize the view.

    3. The main problem is of your variable is too big like ArrayList with images in it some time it can give out of memory exception

提交回复
热议问题