【发布时间】:2018-04-22 17:10:17
【问题描述】:
假设在活动 A(得分)中,我创建了自定义类 Player 的实例。如何将这些对象发送到 Activity C,而无需在 Activity B (SelectGamemode) 中处理它们?这就是我通过 Parcelable 将对象从 Activity A 发送到 B 的方式。
btnNewRound = (Button) findViewById(R.id.btnNewRound);
btnNewRound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Score.this, SelectGamemode.class);
Bundle bundle = new Bundle();
bundle.putParcelable("EXTRA_PLAYER_1", player1);
bundle.putParcelable("EXTRA_PLAYER_2", player2);
bundle.putParcelable("EXTRA_PLAYER_3", player3);
bundle.putParcelable("EXTRA_PLAYER_4", player4);
intent.putExtras(bundle);
startActivity(intent);
}
});
现在我可以像这样在 Activity B 中访问我的 Player 对象:
player1= (Player)getIntent().getParcelableExtra("EXTRA_PLAYER_1");
然后在 Activity B 中,我基本上会使用与 Activity A 中相同的代码将 Player 对象发送到 Activity C,尽管我什至不使用 Activity B 中的对象。如何避免这种情况?谢谢!
【问题讨论】:
-
您可以使用共享首选项或数据库吗?随时随地获取它们。
-
使用共享首选项或数据库,或者通过第二个活动,可能不使用它,然后再到第三个,即使它显然效率较低
-
我去试试数据库,谢谢!
标签: android android-intent android-activity send parcelable