naala11
naala11

Reputation: 5

Passing data between two running activities

I need to pass data from one activity to another. The first time, there is no problem because a new intent is created. My problem is that I use the flag FLAG_ACTIVITY_REORDER_TO_FRONT, because I don't want to destroy the activity since I don't want to go into onCreate every time I switch activity. My question is, how can I pass an array from two running activities?

I am creating a game in android studio, that is similar to Yatzy. In one activity you roll the dices and in the other, you set the score in a scoreboard. Every time the player is navigated to the scoreboard, all possible results are shown, except for the ones that are already chosen (on these places you see the chosen results), and the player can then chose a score, for example, "full house". When I navigate between the two activities I do not want to destroy them since they contain information that I don't want to reset. Therefore I use FLAG_ACTIVITY_REORDER_TO_FRONT, but when I use this the new combinations of the dices are not shown, it only shows the first combination of dices.

Here is when I pass the data from MainActivity:

Intent openMainActivity= new Intent(MainActivity.this, ScoreboardActivity.class);
openMainActivity.putExtra("scoreList", game.showScoreAlt());
openMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
startActivity(openMainActivity)

Here is when I receive the data in the other activity:

Bundle extras = getIntent().getExtras();
scoreList = extras.getIntArray("scoreList");

Upvotes: 0

Views: 529

Answers (1)

vikas kumar
vikas kumar

Reputation: 11028

you can try this Activity.onNewIntent() method.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

Reference.

Upvotes: 1

Related Questions