You are here

Add a message object class

This task describes how to add a message object.

The goal of this application is to have two activities. The first activity displays nodes in a list view. Once an item in the list view is clicked, then a new activity is launched which displays the details for the selected node. The second activity, the node detail activity, needs to know which node has been selected. There are various ways to share such information, but in this example a custom message object is used. This object is passed via an Android Intent from the first to the second activity.

The content of the message object is primarily a node, but because the object needs to be sent between processes, it needs to conform to either the Serializable or Parcelable interfaces. In this example, you will make the message object conform to the Parcelable interface. It should also be noted that the embedded node object has to be Parcelable too. Fortunately, the Alfresco Node class implements both the Serializable and Parcelable interfaces. This simplifies the code required.

  1. Right-click on the TestApp1 project in the Package Explorer window.
  2. Select New > Class.

    The New Java Class dialog is displayed.

  3. In the Package field click Browse. Select the package com.alfresco.tutorials.testapp1.
  4. In the Name text field, enter MessageObject as the name of the class.
  5. In the Interfaces field click Add.

    This displays the Implemented Interfaces Selection dialog.

  6. In the Choose interfaces text field, type Parcelable.

    Parcelable - android.os will appear in the Matching items list.

  7. Select Parcelable - android.os from the Matching items list.
  8. Click OK.
  9. Click Finish.

    The new class is created. It will contain stub methods including describeContents() and writeToParcel().

  10. Modify the code to include the necessary methods:

                
    package com.alfresco.tutorials.testapp1;
    
    import org.alfresco.mobile.android.api.model.Node;
    
    import android.os.Parcel;
    import android.os.Parcelable;
    
    public class MessageObject implements Parcelable {
    	
    	private Node node;
    	// other information as required
    	
    	public MessageObject (Node node){		
    		this.node = node;
    	}
    		
    	public MessageObject(Parcel in){
    		readFromParcel(in);
    	}
    	
    	public Node getNode (){
    		return this.node;
    	}
    	
    	public void setNode (Node node){
    		this.node = node;
    	}
    			
    	@Override
    	public int describeContents() {
    		return 0;
    	}
    
    	@Override
    	public void writeToParcel(Parcel dest, int flags) {
    		dest.writeParcelable(this.node, flags);		
    	}
    	
    	private void readFromParcel(Parcel in){
    		this.node = in.readParcelable(Node.class.getClassLoader());
    	}
    	
    	public static final Parcelable.Creator CREATOR = 
    			new Parcelable.Creator() {
    				
    				public MessageObject createFromParcel(Parcel in){
    					return new MessageObject(in);
    				}
    				
    				public MessageObject[] newArray(int size){
    					return new MessageObject[size];
    				}
    			};
    }
               
                
              

    The message object is essentially a container for a node, although other information could be passed inside the message object if required. The Parcelable object needs to implement certain methods as shown. Simple getter and setter methods allow the node to be set and retrieved.

The MessageObject class has been added to the code. It is a Parcelable object. This will allow a node and other information to be passed between activities.

Sending feedback to the Alfresco documentation team

You don't appear to have JavaScript enabled in your browser. With JavaScript enabled, you can provide feedback to us using our simple form. Here are some instructions on how to enable JavaScript in your web browser.