Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
startActivityForResult() sample code
public class Activity1 extends AppCompatActivity {
public static final int REQUEST_CODE = 255;
protected void onCreate(Bundle savedInstanceState) {
public void sendMessageToActivityOnClick(View view) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra(MESSAGE, "Are you there?");
startActivityForResult(intent, REQUEST_CODE);
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String resultFromActivity2 = data.getStringExtra(RESULT);
// use the result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment