Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
startActivityForResult() sample code
public class Activity1 extends AppCompatActivity {
public static final int REQUEST_CODE = 255;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
}
public void sendMessageToActivityOnClick(View view) {
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra(MESSAGE, "Are you there?");
startActivityForResult(intent, REQUEST_CODE);
}
@Override
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