我的问题是我的两个活动GameActivity和StoreActivity应该共享相同的Player,并且在任何Player中对Activity所做的每个更改都应该在所有活动中看到,但是当我在并从Upgrade更改为购买前对象的状态。StoreActivity为StoreActivity。来自Player的Serializable: LIST_VIEW.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Upgrade upgrade = (Upgrade)LIST_VIEW.getAdapter().getItem(position); if(upgrade.getCost() > player.getMoney()) { LIST_VIEW.getChildAt(position).setEnabled(false); } else { player.buyUpgrade(upgrade); CURRENT_MONEY.setText(getString(R.string.current_money) + player.getMoney()); //Notify GameActivity, but how? } } }); (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 您可以使用Singleton设计模式。您可以使用Player字段从PlayerRepository创建一个对象,然后在您的应用中每次都可以获取该对象你想改变它。 public class PlayerRepository { private Player player; private static final PlayerRepository ourInstance = new PlayerRepository(); public static PlayerRepository getInstance() { return ourInstance; } private PlayerRepository() { } public void setPlayer(Player player) { this.player=player; } public Player getPlayer() { retrun this.player; }}在您的活动中PlayerRepository playerRepository=PlayerRepository.getInstance();Player player=playerRepository.getPlayer();player.buyUpgrade(upgrade); (adsbygoogle = window.adsbygoogle || []).push({});
10-04 19:06