1

I have question about sending object to other activity. Im not sure about this what im doing. So i have object Player in MainActivity

final Player player = new Player("Player", 150);

I have separate class for Player with simple constructor

public class Player {

private String playerName;
private double playerCash;

Player(String playerName, double playerCash)
{
    this.playerName = playerName;
    this.playerCash = playerCash;
}

And i have second Activity , where i want use Player object. I made a button in MainActivity with this code

 mButton = (Button) findViewById(R.id.mButton);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("player", player);
            startActivity(intent);
        }
    });

And now i got problem "Cannot resolve method putExtra". What am i doing wrong? I want only one Player object and want to use it in multiple activities but have no idea how. For any help, big thanks ;)

Rasta_Man
  • 19
  • 4
  • you can not pass an object in putExtra with an intent. Boolean, int, Strings etc are allowed. – drWisdom Feb 16 '16 at 17:32
  • you must implement Serializable on your class and move it as serializable – Vasileios Pallas Feb 16 '16 at 17:34
  • 2
    Your object needs to implement the Serializable or Parcelable interface – Mark Feb 16 '16 at 17:36
  • Possible duplicate of [How to pass an object from one activity to another on Android](http://stackoverflow.com/questions/2736389/how-to-pass-an-object-from-one-activity-to-another-on-android) – Yash Feb 16 '16 at 17:42

4 Answers4

1

Everything that mentioned in the answers above, describe the solution very clearly. Here is the code :

public class Player implements Parcelable{
private String playerName;
private double playerCash;

    // Constructor
    public Player(String playerName, double playerCash){
        this.playerName = playerName;
        this.playerCash = playerCash;
   }
   // Implement Getter and setter methods


   // Parcelling part
   public Player(Parcel in){
       this.playerName = in.readString();
       this.playerCash = in.readDouble();
   }

   @Оverride
   public int describeContents(){
       return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
      dest.writeString(playerName);
      dest.writeDouble(playerCash);
   }
   public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
       public Player createFromParcel(Parcel in) {
           return new Player(in); 
       }

       public Player[] newArray(int size) {
           return new Player[size];
       }
   };
}
karvoynistas
  • 1,295
  • 1
  • 14
  • 30
0

Passing in custom objects is a little more complicated. You could just mark the class as Serializable and let Java take care of this.

However, on the android, there is a serious performance hit that comes with using Serializable. The solution is to use Parcelable. Follow this link for implementation details: http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/

Rohit Sharma
  • 2,017
  • 1
  • 20
  • 22
0

You can use parcelable for that, take a look at my post here: https://stackoverflow.com/a/35252575/982161

since you need to make some changes on the player class.

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

Make one Serializable class

import java.io.Serializable;

@SuppressWarnings("serial") public class MyPlayer implements Serializable {

private String playerName; private double playerCash;

public MyPlayer(String playerName, double playerCash) {
    this.playerName = playerName;
    this.playerCash = playerCash;
}

public String getPlayerName() {
    return playerName;
}

public void setPlayerName(String playerName) {
    this.playerName = playerName;
}

public double getPlayerCash() {
    return this.playerCash;
}

public void setPlayerCash(double playerCash) {
    this.playerCash = playerCash;
}

}

then in your button click put

mButton = (Button) findViewById(R.id.mButton);
mButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        MyPlayer myPlayer = new MyPlayer(playerName,playerCash);
        Intent i = new Intent(MainActivity.this, SecondActivity.class);
        i.putExtra("key", myPlayer);
        startActivity(i);
    }
});

To get Passed Data use(in second activity)

Intent i = getIntent();

MyPlayer myPlayer = (MyPlayer)i.getSerializableExtra("key");

Pratik Tank
  • 2,213
  • 1
  • 17
  • 29