vxhviet
4/7/2016 - 5:26 AM

Sending data back to the CallingActivity

Sending data back to the CallingActivity

Source: StackOverflow

Question: How to send databack to the CallingActivity?

Answer: In CallingActivity, uses startActivityForResult:

public static final int REQUEST_VIDEO = 0;
...
startActivityForResult(new Intent(this, CalledActivity.class), REQUEST_VIDEO);

CalledActivity is launched and you can perform the operation, to close the CalledActivity do this:

public static final String EXTRA_VIDEO_PATH = "com.example.viet.myappdemo.video_path";
...
Intent intent = new Intent();
intent.putExtra(EXTRA_VIDEO_PATH, yourString);
setResult(RESULT_OK, intent);
finish();

CallingActivity - returning from the previous activity will call onActivityResult:

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

    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_VIDEO) {
            String selectedVideoPath = data.getStringExtra(EXTRA_VIDEO_PATH);
            if (selectedVideoPath != null) {
                doOtherStuff();
            }
        }
    }
}