我们来详细讲解一下如何使用Android registerForActivityResult新用法实现两个Activity间数据传递。
什么是registerForActivityResult
registerForActivityResult是Android个10.0之后新增的 API,可以简化 startActivityForResult 的操作,并且更加灵活。通过 registerForActivityResult 可以申请权限、获取图片、视频,等等。
How-To
要使用registerForActivityResult来实现两个Activity间数据传递,我们需要以下几个步骤:
步骤一:创建打开目标Activity的Intent
在目标Activity中,我们需要使用Intent的putExtra()命令在Intent中添加要传递的数据。例如,在MainActivity中打开目标Activity,并将数据传到目标Activity:
private final ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
//处理拿到的数据
}
});
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.putExtra("message", "Hello World!");
mStartForResult.launch(intent);
步骤二:在目标Activity中获取传递的数据
目标Activity中使用getIntent()即可取到传递的数据,例如,在目标Activity中接收来自MainActivity的数据:
String message = getIntent().getStringExtra("message");
步骤三:返回数据到MainActivity中
在目标Activity中,我们可以使用 setResult() 方法返回数据给MainActivity:
Intent intent = new Intent();
intent.putExtra("returnMessage", "Got the message, thanks!");
setResult(Activity.RESULT_OK, intent);
finish();
步骤四:在MainActivity中接收返回数据
我们可以在 onActivityResult() 中获取到返回结果:
private final ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
String returnMessage = intent.getStringExtra("returnMessage");
//处理返回的数据
}
});
示例
示例一:主Activity传递数据给目标Activity
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Button mButton;
private final ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
String returnMessage = intent.getStringExtra("returnMessage");
Toast.makeText(this, returnMessage, Toast.LENGTH_SHORT).show();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
intent.putExtra("message", "Hello World!");
mStartForResult.launch(intent);
});
}
}
TargetActivity.java:
public class TargetActivity extends AppCompatActivity {
private String mMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
mMessage = getIntent().getStringExtra("message");
TextView textView = findViewById(R.id.text_view);
textView.setText(mMessage);
}
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("returnMessage", "Got the message, thanks!");
setResult(Activity.RESULT_OK, intent);
finish();
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...
<Button
android:id="@+id/button"
android:text="Start TargetActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
activity_target.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TargetActivity"
android:textSize="24sp"
android:layout_centerInParent="true" />
<Button
android:id="@+id/button"
android:text="Return to MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text_view"
android:layout_centerHorizontal="true"
android:onClick="onClick" />
</RelativeLayout>
这个示例演示了如何在MainActivity中传递数据到TargetActivity,并在TargetActivity中返回数据到MainActivity。
示例二:目标Activity返回数据给MainActivity
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Button mButton;
private final ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
String returnMessage = intent.getStringExtra("returnMessage");
Toast.makeText(this, returnMessage, Toast.LENGTH_SHORT).show();
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, TargetActivity.class);
mStartForResult.launch(intent);
});
}
}
TargetActivity.java:
public class TargetActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
}
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("returnMessage", "Got the message, thanks!");
setResult(Activity.RESULT_OK, intent);
finish();
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
<Button
android:id="@+id/button"
android:text="Start TargetActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
activity_target.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
<Button
android:id="@+id/button"
android:text="Return to MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="onClick"/>
</RelativeLayout>
这个示例演示了如何在TargetActivity中返回数据到MainActivity。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android registerForActivityResult新用法实现两个Activity间数据传递 - Python技术站