Using Intent for data transmission explanation and examples

How to use Intent to pass data to the next activity:

In fact, it's very simple. When we jump to the page, we just need to use putExtra() method to store the data in Intent, and then use getxxx() method to get the data in the activity after jump

Examples:

Effect display:


Effect analysis: you can see that helloSecondActivity is printed out

Implementation code

1. Modify the code in FirstActivity
public class FirstActivity extends AppCompatActivity {

    //create button object
    private Button startBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        //Binding ID
        startBtn = findViewById(R.id.first_btn);
        //set monitor
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //create will transmit values
                String str = "hello SecondAcitity";
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                //Use putExtra() by key value transmit values
                intent.putExtra("hello",str);
                startActivity(intent);

            }
        });
    }
}
Modify code in SecondActivity
public class SecondActivity extends AppCompatActivity {

    private static final String TAG = "SecondActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        //Get the intent use getIntent() get intent object from startup through getIntent() method
        Intent intent = getIntent();
        //Get data through getStringExtra() use getStringExtra() get values
        String str = intent.getStringExtra("hello");
        //print values
        Log.e(TAG, "onCreate: "+str );

    }
}

over

How to use Intent to return data to the previous activity:

Effect display: AS has some problems, screenshot later

Specific code:

1. Modify the code in FirstActivity:

public class FirstActivity extends AppCompatActivity {

    //create button object
    private Button startBtn;
    private static final String TAG = "FirstActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        //Binding ID
        startBtn = findViewById(R.id.first_btn);
        //set monitor
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                //Use the startActivityForResult () method to start the page Jump, parameter 1:intent object, parameter two: the request code is used for the subsequent callback to determine the source of the data
                startActivityForResult(intent, 1);

            }
        });
    }
    //Rewrite onActivityResult() to get the returned data parameter 1: request code parameter 2: the result parameter passed in when returning data 3: the intent of the returned data
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //Select the requestCode
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String str = data.getStringExtra("hello");
                    Log.e(TAG, "onActivityResult: " + str);
                }
                break;
            default:
        }

    }
}

2. Modify the code in SecondActivity: rewrite the onBackPressed method and listen for whether the return key is pressed

public class SecondActivity extends AppCompatActivity {

    private static final String TAG = "SecondActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);


    }
    //Rewrite onBackPressed() to return data
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        String str = "hello FirstActivity";
        //Create Intent object
        Intent intent = new Intent();
        intent.putExtra("hello",str);
        //The setResult() method returns the processing result parameter 1 to the previous activity: the processing result is generally the result ﹣ OK or the result ﹣ called parameter 2: intent with data
        setResult(RESULT_OK,intent);
        finish();
    }
}

over

Posted on Sun, 05 Apr 2020 05:01:11 -0700 by bradsteele