-1

I have a custom class 'Game' which i init at top of activity code. I then go to another activity, usually i pass arraylists etc but i want to move to passing my custom class.....

My custom class 'game' is a bunch of strings and arraylists with getter and setter mehtods.

i get a

Game is not a parcelable or serializable object

error when I try to add it to the intent. Any ideas what i can do here?

//Init Instance of Game class
Game newGame = new Game();

Set my listener. It works for

//Setup onclick listeners
text.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent i = new Intent(this_Activity.this, next_Activity.class);
        i.putExtra("players", myList);
        i.putExtra("newGame", (Parcelable) newGame);
        startActivityForResult(i, 0);
    }
});
Fearghal
  • 10,569
  • 17
  • 55
  • 97

2 Answers2

6

Also, your class Game may to implement interface Serializable:

public class Game implements Serializable {
    ...
}

You have to change listener in first activity:

text.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent i = new Intent(this_Activity.this, next_Activity.class);
        i.putExtra("players", myList);
        i.putExtra("newGame", newGame);
        startActivityForResult(i, 0);
    }
});

And change method onCreate in next_Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Game newGame = (Game) getIntent().getExtras().getSerializable("newGame");
}
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
1

Game class need to implement Parcelable.

human123
  • 295
  • 1
  • 9
  • Thx for response. Can you expand on your answer. – Fearghal Nov 11 '15 at 12:12
  • Refer https://developer.android.com/reference/android/os/Parcelable.html There you can see that the class MyParcelable is implementing Parcelable. – human123 Nov 11 '15 at 12:15
  • Also refer https://stackoverflow.com/questions/5550670/benefit-of-using-parcelable-instead-of-serializing-object to understand why Parcelable is more preferred than Serializable – human123 Nov 11 '15 at 12:29