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,625
  • 19
  • 68
  • 128
  • 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

3

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,581
  • 10
  • 79
  • 92
  • Partially agree ! If view is independent component (not directly attached to activity) then it can be move between activities. – dharmendra 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. – dharmendra 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

implementing 'Serializable' by itself does not enough, the object must contain Serializable properties. In your case, the code does not work cause ViewGroup is not serializable.

In fact you should not pass views back and front in your application. It is safer to pass only the necessary information and using the information in the view you want to show.

For further information check-out https://www.baeldung.com/java-serialization

Oğuzhan Aslan
  • 166
  • 2
  • 10