Activity之间互相发送数据
activity_send_data_req.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"><Buttonandroid:id="@+id/btn_send_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="@string/send_data" />
</LinearLayout>
SendDataReqActivity.java
package com.example.myapplication;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;public class SendDataReqActivity extends AppCompatActivity {private ActivityResultLauncher<Intent> register;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_send_data_req);// 接收返回数据:注册 ActivityResultLauncherregister = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {if (result.getResultCode() == Activity.RESULT_OK) {Intent data = result.getData();if (data != null) {String returnedData = data.getStringExtra("resultKey");Toast.makeText(SendDataReqActivity.this, "响应数据: " + returnedData, Toast.LENGTH_SHORT).show();}}});// 按钮事件findViewById(R.id.btn_send_data).setOnClickListener(v -> {Intent intent = new Intent(this, SendDataRespActivity.class);// 创建一个包裹Bundle bundle = new Bundle();bundle.putString("dataKey", "你好!!!");intent.putExtras(bundle);register.launch(intent);});}
}
activity_send_data_resp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="vertical"><TextViewandroid:id="@+id/receive_data"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center" /><Buttonandroid:id="@+id/btn_return_data"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="@string/return_data" />
</LinearLayout>
SendDataRespActivity.java
package com.example.myapplication;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;import androidx.appcompat.app.AppCompatActivity;public class SendDataRespActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_send_data_resp);// 获取传递过来的数据Intent intent = getIntent();String receivedData;if (intent != null) {receivedData = intent.getStringExtra("dataKey");if (receivedData != null) {// 将数据显示在页面上TextView textView = findViewById(R.id.receive_data);String show = "接收的数据:" + receivedData;textView.setText(show);}} else {receivedData = "";}// 启动 SecondActivityfindViewById(R.id.btn_return_data).setOnClickListener(v -> {Intent returnIntent = new Intent(SendDataRespActivity.this, SendDataReqActivity.class);returnIntent.putExtra("resultKey", "这是你传给我的数据:" + receivedData);setResult(Activity.RESULT_OK, returnIntent);finish();});}
}
AndroidManifest.xml 加上以下内容
<activityandroid:name=".SendDataReqActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name=".SendDataRespActivity"android:exported="false"></activity>