供自己学习总结使用。
刚开始学安卓开发,startActivityForResult()发现被弃用,网上查询资料之后捣鼓了半天才调好,把registerForActivityResult的使用方法简单记录一下。
实现功能:两个Activity之间交换数据,SecondActivity传数据给FirstActivity,活动均以注册过了。
FirstActivity活动的代码
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);//给当前活动加载一个布局,传入布局的id
//按钮
ActivityResultLauncher requestDataLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
Log.d("FirstActivity",result.getData().getStringExtra("data_return"));
});
Button button_transfer2=findViewById(R.id.transfer2);
button_transfer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
requestDataLauncher.launch(intent);//用的是requestDataLauncher
}
});
//另一个按钮,FirstActivity22直接加了22方便和第一个按钮区分
ActivityResultLauncher requestDataLauncher22 = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
Log.d("FirstActivity22",result.getData().getStringExtra("data_return"));
});
Button button_transfer22=findViewById(R.id.transfer22);
button_transfer22.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
requestDataLauncher22.launch(intent);//用的是requestDataLauncher22
}
});
}
first_layout.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:orientation="vertical">
<Button
android:id="@+id/transfer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="对于下一个活动而言传数据给上一个活动"/>
<Button
android:id="@+id/transfer22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="对于下一个活动而言传数据给上一个活动22"/>
</LinearLayout>
SecondActivity活动的代码(这里和使用startActivityForResult()方法时写的一样)
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
Button button3=findViewById(R.id.button_3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.putExtra("data_return","Hello FirstActivity");
setResult(RESULT_OK,intent);
finish();
}
});
}
}
second_layout.xml代码
<?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=".SecondActivity">
<Button
android:id="@+id/button_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="向上传数据"/>
</LinearLayout>
总结
/*1.使用registerForActivityResult方法,返回ActivityResultLauncher对象 * 2.调用ActivityResultLauncher对象的launch方法,方法传入Intent对象即可完成跳转到下一个活动 * 3.下一个活动finish后,registerForActivityResult方法会监听活动,由result接受返回的数据,用getData方法获取数据的Intent对象即可 * 注:1.如果此活动中有多个组件调用ActivityResultLauncher对象的launch方法来启动不同或相同的活动,那么每个组件都可以独立重复步骤123,返回的数据也都是独立的,不需要请求码。 * 2.registerForActivityResult只能在onCreate中注册。*/