Android StartActivityForResult Example

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. The android startActivityForResult method, requires a result from the second activity (activity to be invoked).

In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

Method Signature

There are two variants of startActivityForResult() method.

Android StartActivityForResult Example

Let's see the simple example of android startActivityForResult method.

activity_main.xml

Drag one textview and one button from the pallete, now the xml file will look like this.

File: activity_main.xml

second_main.xml

This xml file is created automatically when you create another activity. To create new activity Right click on the package inside the src -> New -> Other ->Android Activity.

Now drag one editText, one textView and one button from the pallete, now the xml file will look like this:

File: second_main.xml

Activity class

Now let's write the code that invokes another activity and get result from that activity.

File: MainActivity.java

SecondActivity class

Let's write the code that displays the content of second activity layout file.

File: SecondActivity.java


Output:

android startactivityforresult example output 1 android startactivityforresult example output 2

android startactivityforresult example output 3 android startactivityforresult example output 4





Contact US

Email:[email protected]

StartActivityForResult
10/30