Android: activity data transfer, detailed explanation + super many pictures~

Pass data to the next Activity

1. Create Activity

2. SendActivity sends data, ReceiveActivity receives data

3. Manifest list file setting (SendActivity is the main interface, if you create a class file directly, remember to register! It is enough to imitate frame 1, if you do not register, it will cause a jump to crash)

4. Open the layout file

activity_receive.xml adds TextView as the location for data display

activity_send.xml adds a TextView and a Button, the TextView is the data content, click the Button to jump, and get the text content of the TextView

code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SendActivity">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Hello World"
        android:textSize="26sp" />

    <Button
        android:id="@+id/btn_send_data"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送数据" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ReceiveActivity">

    <TextView
        android:id="@+id/tv_receive"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="26sp" />

</LinearLayout>

5. Operations performed by SendActivity.java

1) Binding control ID

2) Monitor button

3) Initialize the intent (Intent) and define the jump from the current activity to the target activity

4) Initialize Bundle

5) Use putString to put the data into the Bundle

6) Put the Bundle into the intent

7) Turn on the jump

code:

public class SendActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_content;
    private Button btn_send_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        tv_content = findViewById(R.id.tv_content);
        btn_send_date = findViewById(R.id.btn_send_data);
        btn_send_date.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this,ReceiveActivity.class);
        Bundle bundle=new Bundle();
        //传递的数据自己定义,我这边传递的数据是id为tv_content的文本内容
        bundle.putString("request_content",tv_content.getText().toString());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

Tip: When listening to the control, enter this to remind you of an error

Move to the position where the error is reported, Alt+Enter, select the second one, as automatically completes the required code, without typing every time

 6. Operations performed by ReceiveActivity.java

1) Binding control ID

2) Call the getExtras method to get the data

3) To obtain the data whose key is request_content, the key must be named the same as SendActivit to obtain the data

4) Format the obtained data into a string

5) Write the obtained data into TextView

code:

public class ReceiveActivity extends AppCompatActivity {

    private TextView tv_receive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_receive_acitivity);
        tv_receive = findViewById(R.id.tv_receive);
        Bundle bundle=getIntent().getExtras();
        String request_content = bundle.getString("request_content");
        Log.e("Tag",request_content);
        String decs =String.format(request_content);
        tv_receive.setText(decs);
    }
}

Renderings:





 Pass data to the previous Activity

Pass the code first, and then add an explanation later when you have time~

startActivityForResult(); Deprecated, use ActivityResultLauncher to return data

public class RequestActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_request;
    private TextView tv_response;
    private Button btn_request;
    private static final String mRequest = "哈喽哈喽在干嘛!";
    private ActivityResultLauncher<Intent> intentActivityResultLauncher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request);
        tv_request = findViewById(R.id.tv_request);
        btn_request = findViewById(R.id.btn_request);
        tv_response = findViewById(R.id.tv_response);
        btn_request.setOnClickListener(this);
        tv_request.setText("待发送的信息为:" + mRequest);
        btn_request.setOnClickListener(this);
        intentActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result != null) {
                Intent intent =result.getData();
                if (result != null && result.getResultCode() == Activity.RESULT_OK) {
                    Bundle bundle = intent.getExtras();
                    String response_content = bundle.getString("response_content");
                    String decs = String.format(response_content);
                    tv_response.setText("收到回复信息:"+decs);
                }
            }

        });
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this, ResponseActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("request_content", mRequest);
        intent.putExtras(bundle);
        intentActivityResultLauncher.launch(intent);
    }
}
public class ResponseActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_response;
    private TextView tv_request;
    private Button btn_response;
    private static final String mResponse="我在学习呢";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_response);
        tv_response = findViewById(R.id.tv_response);
        tv_request = findViewById(R.id.tv_request);
        btn_response = findViewById(R.id.btn_response);
        tv_response.setText("待应答的消息:"+mResponse);
        Bundle bundle=getIntent().getExtras();
        String request_content = bundle.getString("request_content");
        String decs=String.format(request_content);
        tv_request.setText("收到信息:"+decs);
        btn_response.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_content",mResponse);
        intent.putExtras(bundle);
        setResult(Activity.RESULT_OK,intent);
        finish();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".RequestActivity">

    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送请求数据"
        android:textSize="26sp" />

    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".ResponseActivity">

    <TextView
        android:id="@+id/tv_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="返回应答数据"
        android:textSize="26sp" />


    <TextView
        android:id="@+id/tv_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Guess you like

Origin blog.csdn.net/m0_58241002/article/details/124962223