Android Foundation Points (ongoing updates)

1. Data transfer:

Pass the corresponding data through an Intent object in the form of key->value

public void sendInfo(){
    Intent intent=new Intent(this,ShowActivity.class);//this denotes the current object, and ShowActivity.class denotes the direction in which data is transferred
    //Save data to an Internent object
    intent.putExtra("name",et_name.getText());
    intent.putExtra("password",et_password.getText());
    startActivity(intent);
}

Received in the ShowActivity.java class

//Get Itent Object
Intent intent=getIntent();
//Value
String name=intent.getStringExtra("name");
String password=intent.getStringExtra("password");
//Put the extracted value in the text box
tv_name=(TextView)findViewById(R.id.tv_name);
tv_password=(TextView)findViewById(R.id.password);
tv_name.setText(name);
tv_password.setText(password);

2. Data Callback

What is a data callback?

For example, if there are a1,a2, A2 destroyed, relevant data will be passed back to a1, how to invoke the code reference below:

a2:

  Intent intent=new Intent(this,a1.class);//this denotes the current object, and ShowActivity.class denotes the direction in which data is transferred
    //Save data to an Internent object
    intent.putExtra("name",et_name.getText());
    intent.putExtra("password",et_password.getText());
    setResult(1,intent);/*Explain here: 1: Represents the processing result returned to a1, usually expressed as 0 or 1; intent: Represents the Intent object that needs to be passed, which contains the data you want to pass; setResult is used in conjunction with startActivityForResult(), which indicates the Intent object that needs to be passed, which contains the data you want to pass; setResult is used in conjunction with startActivityForResult to returnIn the class of tones, that is, to use A1*/

a1:

The startActivityForResult() method starts a2 and calls back the onActivityResult() method of a1 when a2 is destroyed, so it needs to be rewritten in a1 to get the returned data, code as follows:

/*Open a2 entry in a1*/
 Intent intent=new Intent(this,a2.class);/*this Represents the current object, and a2.class indicates that the data comes from A2*/
 startActivityForResult(intent,1);/*1: Request code, enter a unique value, but the onActivityResult method below corresponds to it*/
 /*Override onActivityResult() method*/
 protected void onActivityResult(requestCode,resultCode,data){
     if(requestCode==1){
         if(resultCode==1){
             String re=data.getStringExtra("password");
         }
     }
 }

Tags: Java

Posted on Sat, 30 Nov 2019 19:31:14 -0800 by socalnate