How to communicate between two Activities in Android?
An Activity represents the main screen for Android application. An application can have any number of Activities. Intents are message passing mechanisms used in Android. The Android Activity class contains different methods that support Activity communication.
The method startActivity(Intent) is used to invoke one Activity from other Activity. This method accepts an Intent object which specifies the Activity to be started. If the First Activity is expecting some result from Second Activity, a variation of this method is to be used .For example, if you want to start an activity that allows the user to choose a person from the contacts list and when it finishes execution, it will return the selected person. In such cases, the method to be used is startActivityforResult(Intent,int). This method accepts an Intent object and an integer value which is the Request code used to identify the request. When the child finish execution it should call setResult(int) to send data back to First Activity. The argument of setResult(int) method is the Result code which can have constant values like RESULT_OK (operation succeeded),RESULT_CANCELED(operation canceled) etc.. The First Activity should implement onActivityResult(int,int,Intent) method to receive data send by second Activity, where first argument is the Request code,second is Result code and third is the Intent object. Finally the Second Activity should call finish() method to give control back to First Activity. Continue reading