Android global variables

Featured Image © fangol@sxc.hu

Sometimes we need to define global variables accesible from inside our android application to hold some shared values.
Simplest way to implement this is to subclass Android.app.Application class and define static variables that will hold our data.

Inside your Android project, create class called for example “AndroidTutorialApp.java” inside your src folder:

package com.inchoo.tutorial;
 
import android.app.Application;
 
public class AndroidTutorialApp extends Application {
 
	private static AndroidTutorialApp singleton;
 
	public static AndroidTutorialApp getInstance() {
		return singleton;
	}
 
	@Override
	public void onCreate() {
		super.onCreate();
		singleton = this;
	}
}

Don’t forget to enter data inside AndroidManifest.xml:

<!--  AndroidManifest.xml -->
<!--  ... -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.inchoo.tutorial"
    android:versionCode="1"
    android:versionName="1.0"
    android:name=".AndroidTutorialApp"> <!--  Insert name of the class just created -->
<!--  ... -->

Now let’ define some global variable inside AndroidTutorialApp class:

package com.inchoo.tutorial;
 
import android.app.Application;
 
public class AndroidTutorialApp extends Application {
 
	//add this variable declaration:
	public static String somevalue = "Hello from application singleton!";
 
	private static AndroidTutorialApp singleton;
 
	public static AndroidTutorialApp getInstance() {
		return singleton;
	}
 
	@Override
	public void onCreate() {
		super.onCreate();
		singleton = this;
	}
}

And now we can use value of defined variable across our application like this:

package com.inchoo.tutorial;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
 
public class AndroidservicetutorialActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        //This will log our globally defined variable:
        Log.d("MyApp", AndroidTutorialApp.somevalue);
 
    }
}

Hope this will be helpful for someone. Cheers. :-)

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


7 comments

  1. Magnificent items from you, man. I’ve remember your stuff prior to and you are just extremely magnificent. I really
    like what you’ve acquired here, certainly like what you’re saying and the way in which by which you assert it. You’re
    making it entertaining and you continue to care for to stay it sensible. I can’t wait to learn much more from you. This
    is really a tremendous site.

  2. there are 2 serious problems:

    1. don’t have “name” attribute [it’s inside element

    2. static variable defined in ANY class will not survice application restart [when in background Android cleans and restores memory]

  3. I can use static variable in Activity class rather than Application class, so what is difference between that

  4. @Philip Givens, did you think about getting the application context when needed? :-) I am very glad you read this post and now I can also ask you a question why not? It is the native way provided in Android environment.

  5. If you are using static members, why does it matter if you derive from anything at all? Why not just create a static class that derives from object?

    It seems that you were going in two different directions in your post. On one hand, you started out with the idea of a static singleton, but by the time you consumed your static class, you were just using static fields.

  6. Good sumary, but I am still confused about using static final in an Activity class –
    good – because less creation – as I found on some web reviews, but
    bad – because memory leak on rotation.(maybe only bad if static, without final).

    I have done lots of searches over month(s), and noone seems

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.