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. :-)

7
Top

Care to rate this post?

Author

Darko Goles

Ex Inchooer

Darko worked at Inchoo as Backend Developer from March 2011 to January 2014

Other posts from this author

Discussion 7 Comments

Add Comment
  1. Derek Davidson

    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

  2. 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.

  3. @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.

  4. Umar

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

  5. Lol

    @Umar, from application class scope u can access it from another activity for example.

  6. Grzes

    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]

  7. 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.

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top