0

I want to start a other Activity and send a object from a class with one field by type ViewGroup.

Here the Class:

public class SerializableObject implements Serializable {
    public ViewGroup parent;

    public SerializableObject(ViewGroup parent){
        this.parent = parent;
    }

    public ViewGroup getParent(){
        return this.parent;
    }
}

Here the call:

        Intent i = new Intent(thisContext,CordovaViewer.class);
        i.putExtra("KEY",obj);

        cordova.getActivity().startActivity(i);

I get the Error :

Parcelable encountered IOException writing serializable object

Tarasov
  • 3,473
  • 19
  • 63
  • 125
  • http://stackoverflow.com/questions/23142893/parcelable-encountered-ioexception-writing-serializable-object-getactivity – IntelliJ Amiya Feb 24 '17 at 11:36
  • 1
    ViewGroup isn't serializable. – ditn Feb 24 '17 at 11:37
  • have i a other method to send the layout as frameayout to the other activity and replace there the framelyout? – Tarasov Feb 24 '17 at 11:41
  • send the entire layout is a bad practice. post the use case where you need such operation, so that you can find a different solution – arjun Feb 24 '17 at 11:47

3 Answers3

2

You cannot send Views between Activities. Each view is attached to its own Activity (its Context). You risk memory leaks and crashes if you try using a View in one activity that was created with a different one.

Just send the data you need to recreate the object in the receiving Activity.

You can do this with a simple Serializable class that contains only the necessary data or by just setting an Extra for each piece of data you need to pass.

Kuffs
  • 35,313
  • 10
  • 77
  • 92
  • Partially agree ! If view is independent component (not directly attached to activity) then it can be move between activities. – dharam Feb 24 '17 at 12:22
  • A view needs a context to be passed in order for the constructor to be called so it should be impossible to have a view that is not attached to an Activity. You could use the Application context but that would require a very specific use case. – Kuffs Feb 24 '17 at 12:25
  • http://stackoverflow.com/questions/14079719/pass-application-context-to-the-view-instead-of-activity-context – Kuffs Feb 24 '17 at 12:30
  • Yes that would be madness :p . +1 from my side. – dharam Feb 24 '17 at 12:31
0

Your class has to implement Serializable.

intent.putExtra("MyClass", obj);

// To retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");

Hope this helps.

0

Try to pass the serializable list using Bundle.Serializable:

Bundle bundle = new Bundle();
bundle.putSerializable("value", obj);
intent.putExtras(bundle);

And in other Activity get it as:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
Object obj = bundle.getSerializable("value");
dharam
  • 7,739
  • 4
  • 37
  • 70