Articles → Android → Return result from intent in android

Return result from intent in android


Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("name", "karan");
startActivityForResult(intent, 0);

if (getIntent().hasExtra("name")) {
  String name = getIntent().getExtras().getString("name");
  Toast.makeText(SecondActivity.this, name, Toast.LENGTH_LONG).show();

Picture showing how navigation is done from one screen to other using intent

Click to Enlarge

public void finish() {

public void NavigateBack(View view) {
  Intent intent = new Intent();
  intent.putExtra("name", "Karan-Return value");
  setResult(RESULT_OK, intent);


protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

  if (data != null) {
    if (data.hasExtra("name")) {
      Toast.makeText(this, data.getExtras().getString("name"), Toast.LENGTH_LONG).show();

Picture showing how the screen is navigated back using intent

Click to Enlarge

Posted By  -  Karan Gupta
Posted On  -  Friday, November 29, 2019


Your Email Id  
Query/FeedbackCharacters remaining 250