Saturday, January 14, 2012

Passing data to an activity started by an intent

If you need to pass data from one Activity to another using intents, then you have to use the putExtra() methods (there is a general one and then one for almost every data-type) and then getIntent().getExtras() method.

I ran into a problem with this when using the setOnClickListener() method on a Button object to create an onClickListener to with the onClick callback when I got the button by id from the layout xml. I could not pass my data through the intent, maybe because the onClickListener was an anonymous inner class? I'm sure there was a solution to my possible incorrect totally plagiarized code, but then I found an even better solution, which I also plagiarized. Just use the onClick attribute in the layout xml to name the onClick() method in your activity that is the callback for the button. It's much easier and slicker.

See:
http://developer.android.com/reference/android/widget/Button.html
Fork me on GitHub