Creating Global Session Variables for all Activities Android Example

Here is an Android example of how to share variables through out your activities and your project. There is many ways including preferences, extending from a base activity or using  a database to share variables. I like this one because it can be accessed through the whole project and it is easy to implement. The down side is that the variables are only available through a session. For example if the user restarts the application then all the variable will also be cleared. So they can be viewed as session variables.



GlobalState Class:

-Note this extends Application


public class GlobalState extends Application{
	 public File sd = null;
	 boolean refresh = true;
	 pictureData [] picturemarkers = null;
	 public File getFilePath(){
		 return sd;
	 public void setFilePath(File sd){
 = sd;		 

	 public pictureData[] getPictureData(){
		 return picturemarkers;
	 public void setPictureData(pictureData [] picData){
		 picturemarkers = picData;
	 public void setRefresh(boolean refresh){
		 this.refresh = refresh;
	 public boolean getRefresh(){
		 return refresh;

ExampleActivity Class:

public class ExampleActivity extends Activity {

        GlobalState gs;

	public void onCreate(Bundle savedInstanceState) {
              gs = (GlobalState) getApplication();

        public void examples(){  
            File temp = gs.getFilePath();


-I do not fully understand why but it is needed for it to work.  Set the ‘name’ of your application to the path where the file is located.


Posted by in Coding


Permalink 2 Comments

2 responses to “Creating Global Session Variables for all Activities Android Example

  1. Mathijs Segers

    About the I do not fully understand why, is because if you don’t provide this in the Manifest, it will use a generic Application class instead. Since the Application is the instance the app runs on.

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=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on
Fabulou wholesale Petr Mrazek jersey is back. Best choice to get the wholesale Petr Mrazek jersey is suitable for all style of people.