Getting a Result from an Activity| Android startActivityForResult Example Kotlin

Activity in the Android application development, you can do 2-way communication on starting a new Activity. A first Activity can get the result from another Activity. It can be done by call startActivityForResult() method instead of instead of startActivity() method.

get Result from an Activityc Android startActivityForResult

Build an app to get Result from an Activity using Android startActivityForResult method :

Think about one app which has 2 activity, one will be goto another one and 2nd activity will have a button to send some data back to Frist activity.

In this case, you need to override the onActivityResult method, it will that invoked when the second activity returns the result.

Method Signature

See the below example for a better understanding of How to manage startActivityForResult on Android?

Step 1. Create a new project “Build Your First Android App in Kotlin

Step 2. Add following code in “activity_main.xml

In linearLayout there has textView and EditText.

Step 3. Create a new layout file and add the below code.

new layout file: src/layout/activity_second.xml

Step 4. Open the “MainActivity.kt” and add the following code

You must know about the Explicit intent and how to go to one activity to another activity.

Step 5. Create a new Activity and add the below code.

In intent using setResult() method.

Step 7. Add “SecondActivity.kt” in manifest.xml

Step 7. Now Run the application, in an emulator or on your Android device

Output screenshot Android startActivityForResult example:

Android start Activity for Result

Download source code of getting Result from an Activity in Kotlin

https://github.com/EyeHunts/AndroidSaveInstanceState

Q: What is difference between Android startActivityForResult() and startActivity()?

Answer:

startActivity(): start a another activity.

startActivityForResult(): starts another activity from your activity and it expects to get some data from newly started child activity

Q: How to Send data back to the Main Activity in Android?

Answer: The best (and easiest) way is to use an Intent:

See below java code:

MainActivity: Frist Activity call another activity

Simply Override onActivityResult()

MainActivity: Second Activity send data to first activity

Q: What are some examples of getting Results from an Activity?

Answer: Here are some app concept needed a use start Activity For Result:-

  • Camera App: the app can start a camera app and receive the captured photo as a result
  • Recharge App: user to select a contact and you’ll receive the contact details as a result.

Do comment if you have any doubts and suggestions on this tutorial. Write in a comment section if you have any use case suitable for this method.

Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=”25″
TargetSdkVersion=”28″
Coding in Kotlin


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.