FET
FET

Reputation: 942

Get reference to object from another activity

EDIT:

This is the method I use to initialize variables after the layout of the activity is loaded in the onCreate() method

private void initializeVariables() {
    randomButton = (Button) findViewById(R.id.randomButton);
    gameButton = (Button) findViewById(R.id.gameButton);
    i = 1;
    listIndex = 0;
    nameList = PlayersNames.nameList;
    villagerBundle = new ArrayList<>();
    characters = new ArrayList<Card>();
    players = new ArrayList<Player>();


    villagerOne = new Villager();
    villagerTwo = new Villager();
    villagerThree = new Villager();
    villagerFour = new Villager();
    villagerFive = new Villager();

    villagerBundle.add(villagerOne);
    villagerBundle.add(villagerTwo);
    villagerBundle.add(villagerThree);
    villagerBundle.add(villagerFour);
    villagerBundle.add(villagerFive);
}

ORIGINAL QUESTION:

I have 2 activities in Android.

In one i create:

public static Villager villagerOne;

villagerOne = new Villager();

Then in the other one I should access to the villagerOne's mAlive variable:

villagerOne.getMAlive();

For reference, these is the Card class:

public class Card {

//Names
public String mCharacter;

//Status
private boolean mAlive;
private boolean mDefended;
private boolean mOwled;
private boolean mLastSavaged;
private boolean mLastLynched;


//Constructor
public Card(){
}

public void setMCharacter(String value){
    this.mCharacter = value;
}

public void setMAlive(boolean alive){
    this.mAlive = alive;
}

public String getMCharacter(){
    return mCharacter;
}

public boolean getMAlive(){
    return mAlive;
}

}

And this is the Villager class which extends Card:

public class Villager extends Card {

public Villager(){
    mCharacter = "Villager";
}


}

Upvotes: 1

Views: 1315

Answers (3)

Gabe Sechan
Gabe Sechan

Reputation: 93559

If you need a copy of that variable, you should pass it in via extras. If you need to access that actual variable- you probably should rearchitect. Data that's needed by multiple Activities shouldn't be owned by any Activity, it should be owned by another data structure that both Activities could reach. For example a singleton Player object which can be queried for its list of cards.

Upvotes: 0

ilomambo
ilomambo

Reputation: 8350

Since you declared your villager as static in activity one you should be able to access it from anywhere by

one.villagerOne.getMAlive()

(here I assume the name of the activity class one is one)

Since this solution seems to be controversial I'll tell you why I think it is a good one:

If you pass a copy of the Villager through intent and your Villager gets "killed", the original Villager will be destroyed, why the copies will still be alive. this means you have to communicate to every activity with a copy and tell them to defunct their copy of the villager. Listeners and all.

If you use the reference to the static member, as I suggested, all you have to do is test the reference for null before you call any method:

if( one.villagerOne != null ) alive = one.villageOne.getMAlive();

Upvotes: 0

Dennis van Opstal
Dennis van Opstal

Reputation: 1338

I think if you make the Villager class implement Serializable you could send it with an intent like:

Intent i = new Intent(FirstActivity.this, SecondActivity.class);
Villager villagerOne;
villagerOne = new Villager();
i.putExtra("Villager", strName);
startActivity(i);

And on the second activity you get the results:

Villager villager = (Villager) getIntent.getSerializableExtra("Villager");

Upvotes: 1

Related Questions