Rasta_Man
Rasta_Man

Reputation: 19

Passing object to other activity

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 ;)

Upvotes: 1

Views: 77

Answers (4)

karvoynistas
karvoynistas

Reputation: 1285

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];
       }
   };
}

Upvotes: 1

Pratik Tank
Pratik Tank

Reputation: 2252

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");

Upvotes: 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.

Upvotes: 0

Rohit Sharma
Rohit Sharma

Reputation: 2017

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/

Upvotes: 0

Related Questions