How to put a runnable into bundle?
Asked Answered
O

1

9

I want to pass a Runnable into an activity via bundle, so that runnable must run when onCreate fires. I wrote a class which implements Serializable but it causes exception: "java.lang.RuntimeException: Parcelable encountered IOException writing serializable object". My code goes here:

package some.company.common;

import java.io.Serializable;

public class SerializedRunnable implements Serializable, Runnable {

    private static final long serialVersionUID = 6641813629033240205L;

    public SerializedRunnable() {
    }

    private Runnable runnable;

    public SerializedRunnable(Runnable runnable) {
        this.runnable = runnable;
    }

    @Override
    public void run() {
        this.runnable.run();
    }

}
Oedema answered 16/12, 2012 at 8:41 Comment(1)
it is just the contents of the object that are serialized. Why do you want to serialize a runnable, which looks to be a waste. you can always pass just the data to another runnable that will take this data and processIngle
S
0

It looks like Runnable is not serializable. To implement your own serialization you must implement readObject and writeObject by yourself.

Check here

Setula answered 16/12, 2012 at 8:52 Comment(1)
I added two methods just like this: private void readObject(ObjectInputStream aInputStream) throws ClassNotFoundException, IOException { aInputStream.defaultReadObject(); } private void writeObject(ObjectOutputStream aOutputStream) throws IOException { aOutputStream.defaultWriteObject(); } but the same error continues.Oedema

© 2022 - 2024 — McMap. All rights reserved.