Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Types of RequestCode for startActivityforResult

Can anyone kindly list what are the values of requestCode to be passed with startActivityForResult() and their purpose? Also, can you explain on the setResult parameters available like RESULT_OK and what else are there? Kindly help.

like image 903
vinod Avatar asked Sep 03 '12 10:09

vinod


1 Answers

When you launch an activity for result with requestCode >= 0, this code will be returned to the First Activity's onActivityResult() when second activity is finished.You can start multiple Activity for result from your Activity. In each case you get the callback to startActivityForResult() method passing the requestCode. In onActivityResult() we can use the requestCode to find out for which activity we have received the callback. So to differentiate between the callbacks from Activities we use different requestCodes.

For eg:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent i = new Intent(FirstActivity.this, SecondActivity.class);
    startActivityForResult(i, 1);
    Intent i = new Intent(FirstActivity.this, ThirdActivity.class);
    startActivityForResult(i, 2);
}

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

 if (requestCode == 1) {

  if (resultCode == RESULT_OK) {
  //Get the result from SecondActivity
  }

  }
 else  if (requestCode == 2) {
  if (resultCode == RESULT_OK) {
  //Get the result from ThirdActivity
  }
 }
}
like image 50
Nishant Avatar answered Nov 08 '22 21:11

Nishant