Blog moved to

In Uncategorized on March 1, 2012 at 16:02

I moved my blog and website to

Android: Passing objects between activities

In Uncategorized on December 24, 2011 at 12:33

In this post I am going to show you how to easily pass object from one activity to another.

There are many ways how to pass data between activities. It’s easy with primive type such as String, int, boolean… You can use Intent’s putExtra methods.

Problem is when you want pass non primitive object. Your object can implement¬†Parcelable¬† interface or there are many ways. But I didn’t want to implement this interface in every object which I want pass. That’s why I created my own way. I crated one static object which play bridge role between activities. See code bellow.
You have to create only one java class. I called it ActivitiesBridge.

* Help to pass object between activities
public class ActivitiesBringe {

private static Object object;

* set object to static variable and retrieve it from another activity
* @param obj
public static void setObject(Object obj) {
object = obj;

* get object passed from previous activity
* @return
public static Object getObject() {
Object obj = object;
// can get only once
object = null;
return obj;

When you have this class you can use it very easily.
Pass object:

Intent intent = new Intent(this, ClassToWhichIwantToPassObject.class);
ActivitiesBringe.setObject(new MyObject());

Retrieve object:

public class ClassToWhichIwantToPassObject extends Activity {

protected void onCreate(Bundle savedInstanceState) {

MyObject myObject= (MyObject) ActivitiesBringe.getObject();


That’s it. This is how it works for me. It can also work for you. Just try it. It’s very simple.

Blackberry Observer Pattern – Sending events between screens

In Blackberry on July 28, 2011 at 15:52

Sometimes you want to send some information from one screen to another. It can be difficult if there is not good relation between screens. Observer pattern is very helpful. For example I use it to inform screen that popup was closed or to inform screen that thread was finished.
Main goal is to create bus. On bus you can connect or disconnect objects. Connected objects can communicate between each other throw events. I will continue with exaple.

You need to define 2 interfaces. First is Observer. Every object which implements this interface can be connected to the bus, listen and handle events. There is defined method for handling event(message,information) which object receives.

public interface Observer {
public void handleEvent(ObserverEvent event);

Second interface is Observable. Bus will implement this interface.
public interface Observable {

public void addObserver(Observer observer);

public void removeObserver(Observer observer);

public void fireEvent(ObserverEvent event);

First class which we need is EventBus. It represents bus and implements interface Observable.
import java.util.Vector;

public class EventBus implements Observable {

private static EventBus instance;

private Vector observers;

private EventBus() {
observers = new Vector();
//just one instance in application
public static EventBus getInstance() {

if (instance == null) {
instance = new EventBus();
return instance;

* connect observer to eventbus
public void addObserver(Observer observer) {

* disconnect observer from eventbus
public void removeObserver(Observer observer) {

* call handleEvent method on every observer connected to the bus. Every object which implements Observer interface contains handleEvent method
public void fireEvent(ObserverEvent event) {
for (int i = 0; i < observers.size(); i++) {
((Observer) (observers.elementAt(i))).handleEvent(event);


It has one static instance because you want just one bus for application. It contains Vector observers – objects which are connected to the bus. There are methods for add and remove(connect and disconnect) objects. fireEvent will warn objects connected to the bus that there is new event. ObserverEvent class is be described later.

ObserverEvent class wraps what you want send. eventType attribute identifies ObserverEvent. Based on this attribute object can recognise whether event is addressed to him. If you want to send some object you can use attribute content.

public class ObserverEvent {

private int eventType;

private Object content;

public ObserverEvent(int eventType,Object content) {
this.eventType = eventType;
this.content = content;

public int getEventType() {
return eventType;
public Object getContent() {
return content;

public class EventType {

public static final int POPUP_WAS_CLOSED = 1;


Now I will show short example how to use it. I will notify screen that popup was closed.
Screen which want listen events must implement interface Observer

public class MyScreen extends MainScreen implements Observer {

public MyScreen () {

public void handleEvent(ObserverEvent event) {
switch (event.getEventType()) {
case EventType.POPUP_WAS_CLOSED:
//do what you want

Instance of MyScreen is connected to the bus.

MyScreen screen = new MyScreen ();
EventBus.getInstance().addObserver(screen );

Last step is to create ObserverEvent and fire it into bus. For example it can be done in popup screen when button click is handled.

EventBus.getInstance().fireEvent(new ObserverEvent(EventType.POPUP_WAS_CLOSED,null));

That’s it. Event is fired to the EventBus and it notifies connected objects. Connected objects will handle event.