Lifetime of a static variable in Android

前端 未结 14 2258

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:17

    A static variable is associated with the lifecycle of class. Once an activity is destroyed than that static variable will also be cleaned up.

    If you still want to access that variable you have 2 options:-

    1. override onSaveInstanceState and save the required variables and restore it in onRestoreInstanceState
    2. Store the variables in Application class and access it anywhere anytime.

提交回复
热议问题