Skip to content

Instantly share code, notes, and snippets.

@magdamiu
Created May 12, 2020 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magdamiu/0fe027462ad5fcb8fbbbbd692e16b8d4 to your computer and use it in GitHub Desktop.
Save magdamiu/0fe027462ad5fcb8fbbbbd692e16b8d4 to your computer and use it in GitHub Desktop.
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