You are here

Add an application class

In this task you will add a custom Application class to your project.

In earlier tasks you saw how to pass data between two activities using an Intent and a custom message object. It is also possible to share data between activities in other ways. One other way is to use the application context as a means to sharing data. In this way data can be accessed by multiple activities that make up an application. In this task you will learn how to convert your current project into one that uses a custom application object. In later tasks you will use the application context in order to share the session object, and other data as required, across multiple activities.

  1. In the Package Explorer right-click on TestApp1 and select New > Class.
  2. In the Package field enter com.alfresco.tutorials.testapp1.
  3. In the Name field type TestApp1Application.
  4. In the Superclass field click the Browse button to launch the Superclass Selection dialog.
  5. In the Choose a type field type Application.
  6. In the Matching items text area select Application - android.app.
  7. Click OK.
  8. Click Finish.
  9. In this tutorial you are going to share the session object across multiple activities. The session object will be stored in the application context. Modify your code to provide a simple getter and setter for the session object as follows:

                
    package com.alfresco.tutorials.testapp1;
    
    import org.alfresco.mobile.android.api.session.RepositorySession;
    
    import android.app.Application;
    
    public class TestApp1Application extends Application {
    
    	private RepositorySession session;
    	
    	public void setSession(RepositorySession session){
    		this.session = session;
    	}
    	
    	public RepositorySession getSession(){
    		return this.session;
    	}
    }            
                
              
  10. You finally need to modify the AndroidManifest.xml file to incorporate your new Application class. In the Package Explorer double-click on AndroidManifest.xml to load it into the Eclipse editor.
  11. Add the android:name attribute to the application element by modifying the application element as follows:

                  
       <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:name=".TestApp1Application"
            android:theme="@style/AppTheme" >              
                

    Notice that android:name=".TestApp1Application" has been added to the XML.

  12. Save your changes and run your application by right-clicking on TestApp1 in the Package Explorer and selecting Run As > Android Application.

    The application should run as before, you are primarily testing for accidental introduction of errors into your code.