Sending Multiple Intents from a Single Activity to Another Activity

后端 未结 3 1947
南笙
南笙 2021-01-14 14:11

I am very new to android, and I am trying to send user-inputted data (their names) to another activity. I have in the past been able to send single lines between activities

相关标签:
3条回答
  • 2021-01-14 14:33

    You can send multiple paramters to an activity using intent, You don't have to create two different intent for doing this.

    You can do this as follows

    public void sendNames() {
        Intent intent = new Intent (this, MainGame.class);
        EditText player1 = (EditText) findViewById(R.id.player1);
        EditText player2 = (EditText) findViewById(R.id.player2);
        String message = player1.getText().toString();
        String message2 = player2.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        intent2.putExtra(EXTRA_MESSAGE1, message2);
        startActivity(intent);
    }
    

    and then get those values like

    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    String message2 = intent2.getStringExtra(MainActivity.EXTRA_MESSAGE1);
    TextView name1 = (TextView) findViewById(R.id.name1);
    name1.setText(message);
    TextView name2 = (TextView) findViewById(R.id.name2);
    name2.setText(message2);
    
    0 讨论(0)
  • 2021-01-14 14:34

    when i run this i get whatever has been put in for 'name2' in both TextView's

    This is because you are creating a new instance of the Activity with the second Intent. There are different ways you could do it. One would be create a single Intent as a member variable, instantiate it in your first function call, add extras, then add the other extra in the second method, and call startActivity there.

    But it would probably be easier and more readable to just do it all at the same time.

     public void sendNames() {
        //sends player1's name to mainGame
        Intent intent = new Intent (this, MainGame.class);
        EditText player1 = (EditText) findViewById(R.id.player1);
        String player1Name= player1.getText().toString();
        intent.putExtra("player1Name", player1Name);
        EditText player2 = (EditText) findViewById(R.id.player2);
        String player2Name= player2.getText().toString();
        intent2.putExtra("player2Name", player2Name);
        startActivity(intent);
    

    And just call this one method.

    Then get it with

    Intent intent = getIntent();
    String name1 = intent.getStringExtra("player1Name");
    TextView name1 = (TextView) findViewById(R.id.name1);
    name1.setText(message);
    
    String name1 = intent2.getStringExtra("player2Name");
    TextView name2 = (TextView) findViewById(R.id.name2);
    name2.setText(name1 ;
    
    0 讨论(0)
  • 2021-01-14 14:34

    An Intent is like a Map where you can store String (and other serializable objects) with a key. In your code you are using the same key EXTRA_MESSAGE twice.

    Also, keep in mind that you use one Intent to start one Activity: you CANNOT have 2 Intent starting a single instance of an Activity. The solution is to put the 2 values with 2 distinct keys in one Intent.

    0 讨论(0)
提交回复
热议问题