How to start an activity to get response? Or How to start an activity to get re sult?

Answer: startActivityForResult()

Description:
startActivityForResult(Intent intent) is the function to use to start an activity, which returns some result to you on finishing itself.

Start Activity for Result():
We will use this function if we are expecting some result from child activity.
Start Activity for result will take two parameters:
1.Intent.
2.Integer Request code (it is a integer constant)

Returning Results form ChildActivity:
If you want to return success to the parent activity then use below functions.
setResult(RESULT_OK); and then finish();
Once you set the result then you have to immediately call “finish” function.

To return failure:
setResult(RESULT_CANCELED); and then finish();
In case if child activity crashes due to some reason then it will automatically pass RESULT_ CANCELED.
 

How to catch the results from the child in the parent activity:
For this we have to implement a function called onActivityResult(……) in parent activity.
OnActivityResult() function has 3 paramaters
 1.Request code
 2.Result code
 3.Intent.

No comments:

Post a Comment