Passing objects from one Activity to another

When you want to transfer some parameters (String, int, whatever) in Android you can pass it via one of the Intent.putExtra methods. It’s simple as that. You can say:

int parameter = 42; // just an example

Intent intent = new Intent(currentActivity, newActivity.class);
intent.putExtra("myParameter", parameter);
startActivity(intent);

But what happens when you want a pass your custom made class instance? You can pass:

  1. it’s id and gather it from db or web service for example. That’s OK, but it’s a bummer if you have already have it, why do the same thing again?
  2. your class could implement Serializable interface. This is a perfectly working solution, but as many people said, it’s not an optimal solution (just take a look at stackoverflow)
  3. or use android’s Parcelable interface :)

Just to be clear, I don’t like implementing additional serializing/deserializing code, but as it turns out, it’s pretty simple to implement it, and it improves performance. So why not? :)

To make your class Parcelable, you have to implement the following methods:

int describeContents();
void writeToParcel(Parcel dest, int flags);

And you have to define a public static field named CREATOR (that implements Parcelable.Creator<T>)

Just take a look this example:

import android.os.Parcel;
import android.os.Parcelable;

import java.util.Date;

public class ExampleParcelable implements Parcelable {

   private String stringValue;
   private Integer integerValue;
   private char charValue;
   private boolean boolValue;
   private Date dateValue;

   @Override
   public int describeContents() {
      return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(stringValue);
      dest.writeInt(integerValue);
      dest.writeInt(charValue); // yup, it's actually a char
      dest.writeInt(boolValue ? 1 : 0); // can't store bools, but we can do it this way
      dest.writeLong(dateValue.getTime()); // same here, can't write date's, but we can get time in long
    }

    // Added this default constructor in case you are using some JSON/XML whatever parsers that require no-arg constructor
    public ExampleParcelable() { }

    /**
    Just to make life easier, I've added a constructor that creates our ExampleParcelable from a Parcel (of course, you don't have to do it that way)
    */
   public ExampleParcelable(Parcel parcel) {
       // The only important thing is to read them in the same
       // order as you wrote them (take a look at writeToParcel)
       stringValue = parcel.readString();
       integerValue = parcel.readInt();
       charValue = (char) parcel.readInt();
       dateValue = new Date(parcel.readLong());
   }

   public static final Parcelable.Creator CREATOR = new Creator<UserContentInfo>() {

    @Override
    public ExampleParcelable createFromParcel(Parcel source) {
       return new ExampleParcelable(source);
    }

   @Override
   public ExampleParcelable[] newArray(int size) {
      return new ExampleParcelable[0];
   }
};

}

It’s pretty much straight forward. In writeToParcel method you need to write fields that you need (in case you don’t want to pass every field), and later in CREATOR’s createFromParcel read every field in the same order you’ve written to Parcel in the first place.

I’ve added an example how can you write, and later read char and date types. Because can’t write everything (but it can write other Parcelables in case you need it).

Using it is the same thing as in the first example

ExampleParcelable test = new ExampleParcelable(); // just an example, set fields you need

Intent intent = new Intent(currentActivity, newActivity.class);
intent.putExtra("myParameter", test);
startActivity(intent);

Also, in case you are super-lazy to implement your own Parcelables, I’ve found this awesome project (source code: https://github.com/dallasgutauckis/parcelabler).
It creates them for you :)

Advertisement

Super easy iOS XML parsing

I just love it when you need something simple and easy that perfectly suits your needs. I needed some lightweight XML parser for iOS, and I found it.

To be honest (since I’m coming from java background), I didn’t quite fall in love with objective-c at first, and it seemed to be that I needed a lot more code to make some basic stuff work. For example XML parsing. NSXMLParser is ok, but it’s event driven (link #1, link #2). I wanted some dead simple DOM parser.

So the whole library is just two classes SMXMLElement and SMXMLDocument. To show you how simple it is to parse a file, here is a (reduced) snippet from my application:


- (void) parseData:(NSData *) {

   NSError *error;
   SMXMLDocument *document = [SMXMLDocument documentWithData:data error:&error];

   // check for errors
   if (error) {
   DDLogError(@"Error while parsing the document: %@", error);
   return nil;
}

// Array of objects that we are returning
NSMutableArray *result = [[NSMutableArray alloc] init];

// Get the videos node
SMXMLElement *videos = document.root;

// Go through every sub-element "video"
for (SMXMLElement *video in [videos childrenNamed:@"video"]) {
   VideoInfo *info = [[VideoInfo alloc] init];
   info.name = ;

   // Get other values from XML...
   [result addObject:info];
   [info release];
}

return result;
}

A shorter version of XML that the code above parses is:


<videos>
   <video>
      <name>Yup, this a name of a video</name>
   </video>
   <video>
      <name>And another one :)</name>
   </video>
</videos>

As you can see I’m only using (NSString*) valueWithPath:(NSString *) here, but there are a couple more


- (SMXMLElement *)childNamed:(NSString *)name;
- (NSArray *)childrenNamed:(NSString *)name;
- (SMXMLElement *)childWithAttribute:(NSString *)attributeName  value:(NSString *)attributeValue;
- (NSString *)attributeNamed:(NSString *)name;
- (SMXMLElement *)descendantWithPath:(NSString *)path;
- (NSString *)valueWithPath:(NSString *)path;

Do you really need anything else (in some small applications)? :)
Really awesome, kudos to the creators.

Android presentation, prezi and general impression

This is my first post without any code, but I just had to express myself :)

Today I had my first non-JavaSvet presentation (for those who don’t know JavaSvet is the first serbian Java user group). It was organized by the great people in SEE ICT organization. I wasn’t the only speaker,  a friend of mine Vladan Petrović  was in it as well, and he made this awesome presentation in prezi (I’ve helped with the content, not that much with the overall design). You can find the presentation here.

Photo by Vladimir Trkulja

I must say that I am pretty impressed how fast it was to make a presentation, and how effective it looks like. I saw that some people in the audience were impressed. And also, this was my first time presenting something that is inside a browser, and not powerpoint/open(libre)office. So it was all pretty new for me.

The goal was to first explain android basics, and later give them some small exercises that they can work on. We decided to test them with a little bit of tic-tac-toe. We’ve showed all the basics with this little example that I have made (github link). In case you need it, you can find there some basic examples of activities, services, broadcast receivers and content provider. Basically all-in-one example. It’s a small, but functional mp3 player (actual song isn’t hosted @ github). To make things more geeky, I’ve selected Paranoid Android by Radiohead (OK Computer album), cool eh?

The thing that totally blew me away was that when I got back home, I’ve already had some facebook invites, and tweets, that people continued working on the example as their first project.

I would be the luckiest man if any of those guys keep on working, and if they can make some cash out of it, that would be more than amazing. So thumbs up for everybody :)

UPDATE: Great guys from start it uploaded the video to youtube.