How to use global class variables in android

后端 未结 4 925
粉色の甜心
粉色の甜心 2021-01-26 09:12

Hi I am doing one app here. I\'m using global class varibles. It\'s working well, but if I\'m using more globalclass variables I\'m getting memory exceptions some times.

4条回答
  •  被撕碎了的回忆
    2021-01-26 09:58

    First thing, you dont need Static Variable to declare global variable in Application Class,

    so Change your code to:

    class GlobalClass extends Application {
    
       public String myVal;
    
      }
    

    then whereever you need to access this data, get Application object by:

    GlobalClass global=(GlobalClass)context.getApplication();
    global.myVal="anything";
    

提交回复
热议问题