在FirstAcitivity中,进行以下更改
private ActivityResultLauncher requestDataLauncher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode()==RESULT_OK)
{
String data=result.getData().getStringExtra("Ruturn_data");
Log.d("FirstActivity","I got it!");
}
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
Log.d("FirstActivity","haha");
Button button1=(Button)findViewById(R.id.button_1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(FirstActivity.this,"Clicked",Toast.LENGTH_SHORT).show();
// finish();
// String data="Hello Second Activity";
// Intent intent =new Intent(FirstActivity.this,SecondActivity.class);
//显式intent intent.putExtra("extra_data",data);
// startActivity(intent);
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
requestDataLauncher.launch(intent);
}
});
StartActivityForResult()方法惨遭禁用,看了一些文章发现现在都用Result API。
先定义一个ActivityResultLauncher,参数分别为Contract和Callback,在Callback中处理回调结果。
在SecondActivity中,进行一下更改
public class SecondActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
Intent intent=new Intent();
intent.putExtra("Return_data","Hello FirstActivity");
setResult(RESULT_OK,intent);
finish();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
// Intent intent=getIntent();
// String data=intent.getStringExtra("extra_data");
// Log.d("SecondActivity",data);
Button button_2=(Button)findViewById(R.id.button_2);
button_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.putExtra("Ruturn_data","back");
setResult(RESULT_OK,intent);
finish();
}
});
}
}
参考guolin大神的博客,以下为直接复制粘贴
在onActivityResult()方法当中,我们为了区分这个结果是来自之前的哪个任务的,所以要在这里再对requestCode进行判断。
这是以前使用startActivityForResult()方法时的传统写法。
而Activity Result API是没有地方让你传入requestCode的。
我在刚接触Activity Result API的时候受思维惯性的影响被这个问题困扰了一下,没有地方传入requestCode该怎么办呢?
后来思维转过来弯之后发现,原来Activity Result API根本就不需要requestCode这种东西,我们可以使用如下写法来实现和刚才完全一样的功能:
class FirstActivity : AppCompatActivity() {
private val actionViewLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
// Handle result for ACTION_VIEW
}
private val actionDialLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
// Handle result for ACTION_DIAL
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_first)
val firstButton = findViewById<Button>(R.id.first_button)
val secondButton = findViewById<Button>(R.id.second_button)
firstButton.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW)
actionViewLauncher.launch(intent)
}
secondButton.setOnClickListener {
val intent = Intent(Intent.ACTION_DIAL)
actionDialLauncher.launch(intent)
}
}
}
原文链接:https://blog.youkuaiyun.com/guolin_blog/article/details/121063078