An android app user interface is made up of activities and fragments. At some point you may need to navigate from one activity to another. In this tutorial we will discuss how to navigate from one activity to another with a data passed to next activity. Also to return with a data to previous activity. If this doesnt make any sense, Follow along the tutorial and you will understand.
What we are going to do
Create two activities
send data from first activity to second activity
return data from second activity to first activity
Lets start by creating new project
Im simply creating a project with name "ActivityTransition". In the project creation dialog, choose empty activity and rename MainActivity to FirstActivity and make sure its selected as launcher activity. Then click finish and wait for it finish build. Now We need to create second activity and name it SecondActivity.
To get a simple user interface as below simply use the code below.If you prefer typing simply type it.
Or if you want to retreive result in first activity that is to send data from second activity back to first activity, use startActivityForResult method instead of startActivity and implement onActivityResultMethod. you can implement by pressing ctrl + O and search onactivityresult or simply pase below code. Pass a intent code so that you can use in onActivityResultMethod that is implemented in First Activity.Use below code instead of startActivity