Passing an object from one application to another

I need to pass a custom object from one application to the main activity of another.

Will I be able to accomplish this with aim.putExtra like below:

putExtra("key",obj);

      

There was an error because the putExtra method does not support this. How can this be achieved?

Any help is appreciated.

EDIT

Next instance of the class, I need to move to another application. I would have an obj object like this follows me:

CounterAndNotifier obj = new CounterAndNotifier(5, this)

      

obj - the object to be passed

public class CounterAndNotifier
{
private int counter = 0;
private int count;
private Runnable runnable;

public CounterAndNotifier(int count, Runnable runnable)
{
    this.count = count;
    this.runnable = runnable;
}

public synchronized void incrementCounter()
{
    counter++;
    if (counter == count) {
        runnable.run();
    }
}
}

      

0


source to share


1 answer


Update 2: Constructor example:

CounterAndNotifier counter = new CounterAndNotifier(0, new SerializableRunnable() {
            @Override
            public void run() {

            }
        });

      

or

SerializableRunnable runnable = new SerializableRunnable() {
        @Override
        public void run() {
            /* Running code here */
        }
});
CounterAndNotifier counter = new CounterAndNotifier(0, runnable);

      

Update: Runnable is an interface and is not Serializable per se. To work around this, create a new class like this:

public abstract class SerializableRunnable implements Runnable, Serializable {
    /* Nothing needs to go here */
}

      

Then change your Runnable references to SerializableRunnable and you can serialize your object in intent without issue.

Your new object will look like this:

public class CounterAndNotifier implements Serializable
{
    private int counter = 0;
    private int count;
    private SerializableRunnable runnable;

    public CounterAndNotifier(int count, SerializableRunnable runnable)
    {
        this.count = count;
        this.runnable = runnable;
    }

    public synchronized void incrementCounter()
    {
        counter++;
        if (counter == count) {
            runnable.run();
        }
    }
}

      



You can still create Runnable objects the same way, just with a different name.

Original answer: You cannot add a normal object to Intent

extra. If your object members are serializable types, you can simply implement the interface Serializable

and then you can put the object in Intent

. Otherwise, you can implement Parceable

and flatten your object in the premise.

Usage Serializable

requires much less code changes if your object members are base types. Also, you should check if it has been confirmed by the receiving application to determine what you should be using.

It will look something like this:

public class YourObject implements Serializable {

    private static final long serialVersionUID = -6242440735315628245L;
    /* Your class members */

}

      

Adding to the goal:

Intent intent = new Intent();
intent.putExtra("YourKey", new YourObject());

      

Exiting intent:

YourObject obj = (YourObject)intent.getSerializableExtra("YourKey");

      

+2


source







All Articles