RequestCode types for startActivityforResult

Can someone specify a list of requestCode values ​​that will be passed using startActivityForResult() and their purpose? Also, can you explain the setResult options available as RESULT_OK , and what else is there? Please help.

+6
source share
4 answers

When you start an action for a result using requestCode >= 0 , this code will be returned to the first onActivityResult() activity when the second action is completed. You can run multiple Activity for the result from an Activity . In each case, you get a callback to the startActivityForResult() method that passes the Code request. In onActivityResult() we can use requestCode to find out what activity we received the callback for. Thus, to distinguish callbacks from Activities , we use different requestCodes.

For instance,

 @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 } } } 
+7
source

You can put whatever you want in requestCode so that you know what specific information you expect to return after starting Activity with the specified requestCode . The called Activity should call setResult(RESULT_OK) when it passed the information to the previous Activity to make sure that this is the correct data, and everything went fine.

+2
source

requestCode requestCode used to allocate the identifier for the request so that the request can be identified with this code in onActivityResult() . For example, if someone wrote codes to trigger two actions in action B and C, following the code

 startActivityForResult(new Intent(A.this, B.class), 1); startActivityForResult(new Intent(A.this, C.class), 2); 

now in onActivityResult() you can recognize which Activity returned the result.

The setResult() method is used to set a Intent to Result and a resultCode . Through resultCode we will tell onActivityResult() that the result is approved or canceled. in Intent , which we set to Result, can be used to transfer some data using the intent.putExtra() methods.

+2
source

requestCode up to you. This will help you find out which Activity finished in your onActivityResult() method. In addition, the result parameters more or less correspond to you if you work only with your own actions. You can consider this as the return value of the called activity for the call.

+1
source

Source: https://habr.com/ru/post/924434/


All Articles