Android StartActivityForResult ExampleBy 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 SignatureThere are two variants of startActivityForResult() method. Android StartActivityForResult ExampleLet's see the simple example of android startActivityForResult method. activity_main.xmlDrag one textview and one button from the pallete, now the xml file will look like this. File: activity_main.xml
second_main.xmlThis 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 classNow let's write the code that invokes another activity and get result from that activity. File: MainActivity.java
SecondActivity classLet's write the code that displays the content of second activity layout file. File: SecondActivity.java
Output:![]() ![]() ![]() ![]()
Next TopicAndroid Share App Data
|
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week