0

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.

I tried this:

  public class SecondClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);



    TextView tv = (TextView)findViewById(R.id.tv);
    tv.setText("Global String : " + GlobalClass.myVal);
}
 }


  class GlobalClass extends Application {

   static String myVal;

  }

Is this correct or not?

AMAN77
  • 6,218
  • 9
  • 45
  • 60
user1089640
  • 115
  • 1
  • 3
  • 14

4 Answers4

4

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";
jeet
  • 29,001
  • 6
  • 52
  • 53
  • extending Activity is a bad idea : the activity class is here to allow you to display something; with a full lifecycle associated with this task. The Application class is widely used to store global app variables like @jeet is doing. It has one pitfall though : the Application object can be destroyed during the lifecycle of your app (mostly if the system needs the memory), so don't assume that the Application class will always safely keep all your variables or you will encounter weird errors when it is not the case. – Teovald Oct 29 '12 at 10:35
  • its working fine.now also my app outofmemory exception is coming some times – user1089640 Oct 29 '12 at 13:33
  • @Teovald what should i use ? sharedPrefernce? – Swap-IOS-Android Sep 11 '14 at 09:45
  • sharedPreferences can be very useful to store a simple object between sessions. For example if you want to display a tutorial the first time the user users the app. – Teovald Sep 11 '14 at 12:47
3

You can use like this

public class GlobalVar {

    public int getMyVar() {
        return myVar;
    }

    public void setMyVar(int myVar) {
        this.myVar = myVar;
    }

    private int myVar = 0;
    private static GlobalVar instance;

    static {
        instance = new GlobalVar();
    }

    private GlobalVar() {
    }

    public static GlobalVar getInstance() {
        return GlobalVar.instance;
    }

}

then you can call like

GlobalVar.getInstance().setMyVar(int);
Devangi Desai
  • 1,355
  • 12
  • 17
1

You can also use Global Variable Activity class wise. As for example

 public class SecondClass extends Activity {
String S1,S2,S3;
EditText edt1,Edt2,Edt3;
Button btn1,btn2,btn3;
//like this wat Declare all variable you want to use in your Present Activity Class
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.second);
edt1= (EditText)findViewById(R.id.tv);
}
}
SAURABH_12
  • 2,262
  • 1
  • 19
  • 19
0

Take a look at the post Singletons vs. Application Context in Android?

There are a lot of discussion about Singletons vs Application objects in the forum. I'm personally inclined to Application object with properties. If you dont want to keep in memory a lot of objects use a LruCache (there is a pre v11 implementation in the compatibility package) to low your memory requirements.

Take into account you will eat the same amount of memory using Singletons than using the Application object, all objects will be keep in memory until you free them (remove any refrence to them and let the GC purge them from memory).

Community
  • 1
  • 1
rgrocha
  • 1,461
  • 10
  • 19