Articles → Android → Return result from intent in android

Return result from intent in android






Example








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



@Override
public void finish() {
  super.finish();
}

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

  finish();
}




@Override
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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250