我想知道是否可以处理来自例如活动1中的活动2和活动3有一个onActivityResult(),或者我需要为每个返回数据的活动都有一个方法?
活动1是应用程序的主要活动.
活动1:
// Handle return value from activity protected void onActivityResult(int requestCode,int resultCode,Intent data) { if (resultCode == Activity.RESULT_OK) { String imageId = data.getExtras().getString("imageId"); // Do something if data return from activity 2 ?? // Do something if data return from activity 3 ?? } }
活动2
Intent intent = new Intent(); intent.putExtra("imageId",imagePath); setResult(RESULT_OK,intent); finish();
活动3
Intent intent = new Intent(); intent.putExtra("contactId",data); setResult(RESULT_OK,intent); finish();
解决方法
在startActivityForResult中为activity 1设置requestCode:
呼叫活动2
Intent intent = new Intent(this,Activity2.class); startActivityForResult(intent,10);
呼叫活动3
Intent intent = new Intent(this,Activity3.class); startActivityForResult(intent,11);
现在,当你来onActivityResult检查requestCode
喜欢:
public void onActivityResult(int requestCode,Intent data) { super.onActivityResult(requestCode,resultCode,data); switch (requestCode) { case (10): { // do this if request code is 10. } break; case (11): { // do this if request code is 11. } break; }