如何在 Android 上管理 startActivityForResult?

在我的活动中,我正在通过startActivityForResult从主活动中调用第二个活动。在我的第二个活动中,有一些方法可以完成此活动(可能没有结果),但是只有其中一个返回结果。

例如,从主要活动中,我叫第二个活动。在本活动中,我将检查手机的某些功能,例如是否带有摄像头。如果没有,那么我将关闭此活动。另外,在准备MediaRecorderMediaPlayer如果发生问题,那么我将关闭此活动。

如果其设备带有摄像头并且录制已完全完成,那么在录制视频后,如果用户单击 “完成” 按钮,我会将结果(录制的视频的地址)发送回主要活动。

如何检查主要活动的结果?

答案

Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}//onActivityResult

如何检查主要活动的结果?

您需要覆盖Activity.onActivityResult()然后检查其参数:

  • requestCode标识哪个应用返回了这些结果。这是由您在调用startActivityForResult()时定义的。
  • resultCode通知您此应用是否成功,失败或其他不同
  • data包含此应用返回的所有信息。这可能为null
Intent returnIntent = getIntent();
returnIntent.putExtra("result",result);
setResult(RESULT_OK,returnIntent);
finish();
new Intent();
getIntent();
public class MainActivity extends AppCompatActivity {

    // Add a different request code for every activity you are starting from here
    private static final int SECOND_ACTIVITY_REQUEST_CODE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    // "Go to Second Activity" button click
    public void onButtonClick(View view) {

        // Start the SecondActivity
        Intent intent = new Intent(this, SecondActivity.class);
        startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE);
    }

    // This method is called when the second activity finishes
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // check that it is the SecondActivity with an OK result
        if (requestCode == SECOND_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) { // Activity.RESULT_OK

                // get String data from Intent
                String returnString = data.getStringExtra("keyName");

                // set text view with string
                TextView textView = (TextView) findViewById(R.id.textView);
                textView.setText(returnString);
            }
        }
    }
}
public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }

    // "Send text back" button click
    public void onButtonClick(View view) {

        // get the text from the EditText
        EditText editText = (EditText) findViewById(R.id.editText);
        String stringToPassBack = editText.getText().toString();

        // put the String to pass back into an Intent and close this activity
        Intent intent = new Intent();
        intent.putExtra("keyName", stringToPassBack);
        setResult(RESULT_OK, intent);
        finish();
    }
}

对于那些在 onActivityResult 中的 requestCode 错误的问题

如果您从Fragment调用startActivityForResult() ,则拥有该 Fragment 的 Activity 会更改 requestCode。

如果要在活动中获取正确的 resultCode,请尝试以下操作:

更改:

startActivityForResult(intent, 1);至:

getActivity().startActivityForResult(intent, 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_CANCELED) {
        return;
    }

    global_lat = data.getDoubleExtra("LATITUDE", 0);
    global_lng = data.getDoubleExtra("LONGITUDE", 0);
    new_latlng = true;
}

@Override
protected void onResume() {
    super.onResume();

    if(new_latlng)
    {
        PhysicalTagProperties.this.setLocation(global_lat, global_lng);
        new_latlng=false;
    }
}

首先,您将startActivityForResult()与第一个Activity参数一起使用,如果要将数据从第二个Activity发送到第一个Activity然后使用IntentsetResult()方法传递值,并在第一个Activity onActivityResult()方法中获取该数据。

Intent i = new Intent(A.this, B.class);
startActivity(i);
b1......clickListener
{
   Intent resultIntent = new Intent();
   resultIntent.putExtra("Your_key","Your_value");
   setResult(RES_CODE_A,resultIntent);
   finish();
}
b2......clickListener
    {
       setResult(RES_CODE_B,new Intent());
       finish();
    }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RES_CODE_A) {

     // b1 was clicked 
   String x = data.getStringExtra("RES_CODE_A");

}
else if(resultCode == RES_CODE_B){

   // b2 was clicked

}
else{
   // back button clicked 
}
}
You need to override Activity.onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_CODE_ONE) {


   String a = data.getStringExtra("RESULT_CODE_ONE");

}
else if(resultCode == RESULT_CODE_TWO){

   // b was clicked

}
else{

}
}