How to declare a global variable in Android – Eclipse – Java

If you are an android developer and you just started making new apps in android. You may come across with an issue “How to declare a global variable which you can access in whole application”.  Let me show you how you can achieve it easily:

Steps for Declaring Global Variable:

1.Create a class that extends the Application class:

2. Add the class to the AndroidManifest file as an attribute of <application> tag:

3. ou can access it from any context using the Context.getApplicationContext() method. Now, we can access the global data across the application like:

 

Sana Beginner Programmer Asked on March 15, 2014 in General Question.
Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.