0

Let's say in Activity A (Score) I create instances of my custom class Player. How can I send these objects to Activity C, without needing to deal with them in Activity B (SelectGamemode)? This is how I send the objects via Parcelable from Activity A to 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);
    }
});

Now I would access my Player objects in Activity B like this:

player1= (Player)getIntent().getParcelableExtra("EXTRA_PLAYER_1");

Then in Activity B I would basically use the same code as in Activity A to send the Player objects to Activity C, although I don't even use the objects in Activity B. How can this be avoided? Thank you!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kubi
  • 73
  • 6

1 Answers1

0

It sounds like you currently have this activity flow:

A -> B -> C

Perhaps a better flow would be:

A -> B
B returns result to A
A -> C

I'm extrapolating from some small bits of your question, but it sounds like Activity A has the job of creating players, and Activity B has the job of choosing game modes. Then Activity C takes both the players and the selected game mode and does something with them. Here's some pseudo-code for how you could apply my suggestion to that situation:

// in Activity A
public void startChooseGameMode() {
    Intent intent = new Intent(this, ActivityB.class);
    startActivityForResult(intent, 123); // 123 is an example requestCode
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (reqeustCode == 123) {
        if (resultCode == Activity.RESULT_OK) {
            GameMode mode = data.getParcelableExtra("EXTRA_GAME_MODE");

            Intent intent = new Intent(this, ActivityC.class);
            intent.putExtra("EXTRA_GAME_MODE", mode);
            intent.putExtra("EXTRA_PLAYER_1", player1);
            intent.putExtra("EXTRA_PLAYER_2", player2);
            intent.putExtra("EXTRA_PLAYER_3", player3);
            intent.putExtra("EXTRA_PLAYER_4", player4);

            startActivity(intent);
        }
        else {
            // handle the error/cancellation
        }
    }
}

// in Activity B
public void chooseGameMode(GameMode mode) {
    Intent data = new Intent();
    data.putExtra("EXTRA_GAME_MODE", mode);

    setResult(Activity.RESULT_OK, data);
    finish();
}

In some ways, this is the same problem as your original question, just in a different direction; now Activity A has to know about the GameMode result from Activity B, even though it doesn't use it directly. I think that this is ok. You can think of Activity A as being responsible for gathering all the data necessary to start Activity C... perhaps in the future, you'll create players in a different way, and offload that work to a fourth activity.

Ben P.
  • 52,661
  • 6
  • 95
  • 123