What is use of start activity for result in Android? Please give example and what is difference between startactivity and startactivityforresult?
By the help of android startActivityForResult() method, we can get result from another activity. By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa.
0 . It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.
Starting activities or services. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent.
What are return types of startActivityForResult in android Options 1 RESULT OK 2 RESULT CANCEL 3 RESULT CRASH 4 A.
By calling startActivityForResult
with Activity2
, your current activity will be notified when the Activity2
is finished (back button pressed), and this way you can also get information from it.
This notification you can catch by overriding your activity's onActivityResult
method.
This article about Android startActivity and startActivityForResult might be worth to look at.
startActivityForResult()
allows you to start activity and get some data back. Imagine that you have some file picker activity. You can start it and when user chooses the file, the result is given back to the original activity.
Also, it can be used if you simply want to ensure that the second activity has successfully done somethings.
The result code is obtained in onActivityResult method:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Result OK.d.
if (requestCode == resultCode) {
// do something good
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With