wilbomc
wilbomc

Reputation: 183

Multiple Intents in a Single Activity

Having issues with sending multiple pieces of data (in this case, three arrays(two int, one string)) over to a second activity page.

I am unsure of how this is done. What I would like to know is how to send these arrays in one Start Activity method, if that is possible. My current code is:

public void onClickGoToTeamSummary(View view)
{
    Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class);
    Intent intentTeamPoints = new Intent(MainActivity.this, ResultsActivity.class);
    Intent intentTeamGoals = new Intent(MainActivity.this, ResultsActivity.class);

    intentTeamNames.putExtra("footballClubs", myTeams);
    intentTeamPoints.putExtra("clubPoints", pointsAttained);
    intentTeamGoals.putExtra("clubGoals", goalsScored);

    startActivity(intentTeamPoints);
    startActivity(intentTeamNames);
    startActivity(intentTeamGoals);
}

I had tried:

startActivity(intentTeamPoints, intentTeamNames, intentTeamGoals);

to no avail. To help, my getIntent in the next activity looks like this:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);

    Intent intentClubNames = getIntent();
    String[] club_names = intentClubNames.getStringArrayExtra("footballClubs");

    Intent intentClubPoints = getIntent();
    int[] team_points = intentClubPoints.getIntArrayExtra("clubPoints");

    Intent intentTeamGoals = getIntent();
    int[] club_goals = intentTeamGoals.getIntArrayExtra("clubGoals");
}

The code itself works provided only one startActivity is used. I would like to know how to pass all my arrays into the second activity page through one activity if anyone can help me.

Upvotes: 0

Views: 1955

Answers (2)

codeMagic
codeMagic

Reputation: 44571

You don't use multiple Intents but multiple extras.

// create your Intent as normal
Intent myIntent = new Intent(MainActivity.this, ResultsActivity.class);

// then you can add multiple extras
myIntent.putExtra("footballClubs", myTeams);
myIntent.putExtra("clubPoints", pointsAttained);
myIntent.putExtra("clubGoals", goalsScored);
startActivity(myIntent);

Then receiving them would be the same. You would just receive the one Intent and use the key as normal for each extra Array.

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

You can send all values in single Intent

public void onClickGoToTeamSummary(View view)
{
    Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class);

    intentTeamNames.putExtra("footballClubs", myTeams);
    intentTeamNames.putExtra("clubPoints", pointsAttained);
    intentTeamNames.putExtra("clubGoals", goalsScored);

    startActivity(intentTeamNames);
}

and

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);

    Intent intentClubNames = getIntent();
    String[] club_names = intentClubNames.getStringArrayExtra("footballClubs");
    int[] team_points = intentClubNames.getIntArrayExtra("clubPoints");
    int[] club_goals = intentClubNames.getIntArrayExtra("clubGoals");
}

Upvotes: 2

Related Questions