1

I have the following Classes:

public class Creature implements Serializable {
protected String name;

public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}
// Rest of the methods and variables
}

Player Class extends Creature:

public class Player extends Creature implements Serializable {
// Class code
}

I am passing an instance of Player from one activity to another using Intents:

// Activity A, passing the object
Intent charActivity= new Intent(this, CharacterActivity.class);
charActivity.putExtra("Player", player);
startActivity(goToCharacterActivity);

// Activity B, grabbing the object
Player player = (Player) getIntent().getSerializableExtra("Player");

Now if I do something like player.setName("Hello"); in activity B the "original" object name doesn't update, the new name will be valid only in this current activity, because I only have a local "copy" of the object. How do I go around this if I must use Serializable?

Haris
  • 138
  • 1
  • 11
  • You can't do that, you need to use some other method to pass information between activities see https://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities – Oleg Oct 31 '17 at 00:14
  • use startActivityForResult(goToCharacterActivity) and pass this object to orginal activity in new activity using setResult method – Cyrus Oct 31 '17 at 00:49
  • @Cyrus I have tried that, when I am overriding `onActivityResult` I can get the `player` object passed from the activity B however I can't assign it to my variable in the `Activity A` The scope is only local, once the `onActivityResult` is out of scope the variable points to the previously assigned object... – Haris Oct 31 '17 at 15:37
  • @HarisK. Why don't your change your local variable to global variable . what's more , according to my experience ,only updating member would be passed not whole object . – Cyrus Nov 01 '17 at 00:40

4 Answers4

0

If you want them to reference the same Object in memory you cannot use Serializable or Parcelable. (A new Object will automatically be created in memory).

In this case I'd recommend having a shared class with static fields that hold your information. E.g.:

SharedInfo:

public class SharedInformation {
    public static Creature theCreature;
} 

Activity A: start using the Intent as usual, but don't pass the Person or Creature

Activity B:

SharedInformation.theCreature.setName("Hello"); 
0

I have one example with a List of players in github. What I did is to use onActivityResult and startActivityForResult methods those allows to return values between two activities. You can see the full example and download the code from my github repoitory.

Saul Rosales
  • 139
  • 1
  • 5
0

Because your intent name is not the same .

Intent charActivity= new Intent(this, CharacterActivity.class);

You use charActivity as the name .

Change

startActivity(goToCharacterActivity);

to

  startActivity(charActivity);
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
  • This was a copy and paste error from my code when I was renaming my variables to better explain the problem. This is working as intended in my code, the problem lies in the fact that modifying `player` inside the `charActivity` doesn't change the object in the other activity. This is what I want to do.. – Haris Oct 31 '17 at 13:46
0

if u want passing between Activity try Bundle like this

Activity 1 :

Intent i =  new Intent(this,CharacterActivity.class);
Bundle playerBundle = new Bundle();
playerBundle.putSerializable("Player",player);
i.putExtras(playerBundle);
startActivity(i);

and this is how to retrieve the bundle at Activity 2:

Player player = (Player) getIntent.getSerializableExtra("Player");

and remember the key between that intent must same in this example i'm using

  • Player

as key

MOF
  • 163
  • 1
  • 10