How to Switch Activity in android and Pass Data between them using Android Studio

Switch Activity Android and Pass Data between them Using New Android Studio:

It is quite common to switch between activities / screens in android. We use different activities / screens in our app for different purposes e.g., to display login screen in one activity and to display login success message in another activity, to display a list of items in one activity and to show the details in another activity when user clicks on one of the list item etc… In this Article, we will discuss about how to switch an Activity in Android using Android Studio and also how to pass data between them.switch_activity

Here, in this example, we will create a subscription form in which user fills in his name and email in the first activity and we will display a thank you note in the second activity mentioning his name and email entered by him in the first activity. Sounds great? Let’s get started!

Before diving into code, let us examine the code we mainly use in this tutorial for switching activities:

Code used to start activity:

We use startActivity() method to start a new activity from the current activity. startActivity takes Intent as its parameter. Intent can be thought as an operation to be performed.

Here Intent takes two parameters – context and class.

We can also use startActivityForResult() to start a new activity but we use this method if we expect any data to be returned back from our next activity(e.g., taking picture in one activity and send it to another activity).

 

Code used to send the data to the next activity:

 

We store the data to be sent to the next activity in the Intent. To store the data we use the method putExtra() from the Intent class. putExtra() method takes two parameters- key and value pair. This key is required by the activity which wants to get the data from the the previous activity.

Code used to retrieve the data from the previous activity:

 

We use getStringExtra() method from Intent class to retrieve the data stored in the Intent. If we want to retrieve a Boolean value we use getBooleanExtra(). They takes key as a parameter. We need to pass the exact key that we used in putExtra() to retrieve the data from the previous activity’s intent.

Switch Activity in Android Using Android Studio:

Prerequisites:

1. How to create Android Studio Project

2. Knowledge on Code snippets discussed above.

The complete code can be downloaded from the link below:

 

Creating an XML file:

  1. First we define our xml file for to inflate our first layout which consists of two EditTexts – one for entering name and other to enter our email.
  2. We will also Define Two TextViews above each of the EditTexts to specify what user has to enter in the following EditText fields.
  3. Next we define a Button (In this example I used an ImageButton) so that when user hits that button it takes him to the next activity.

Here is the following XML layout file:

 

Next, we define our second layout which consists only of a TextView whose text will be set in our Java Class. The following is the layout for our second activity. Here I named it as activity_second.xml.

 

We now move on to Java class and start wiring up the EditTexts, and Buttons and also make our activity to do our work. Here I created a new Class by right clicking on PackageName and selecting New > Java Class. I named my first class as FirstActivity.java.

 

Now, in this activity we inflated our activity_first.xml layout and wired up the Buttons and EditTexts. Then we used OnClickListener() method on our subscribe button and we started anonymous class as its parameter.

In our Anonymous class, we stored the data provided by the user such as name and email in the intent by using putExtra() method which takes key and value pair.

We defined two strings EXTRA_NAME, EXTRA_EMAIL  whom we will use as keys in our putExtra() methods.We next passed the intent to startActivity() as its parameter.

startActivity knows which activity to start from the following code:

 

Here, Intent() consists of two parameters – Context and Activity which needs to be started next.

Here thus we completed our FirstActivity.java. Make sure you add the reference to Activity in Manifest by using the following code:

 

Let us create another activity and name it as SecondActivity.java.

 

We need to access the data we stored in our intent from our FirstActivity. This data is accessed by using the key declared in putExtra method. We retrieve the data by the following code:

String name= getIntent().getStringExtra(FirstActivity.EXTRA_NAME);

Next we set the text of the textview using setText() method. You must reference each new activity in your manifest file. So add the following code in between your manifest’s <application> tag.

 

Now we successfully switched activities in android as well as passed the data between them.

Vamsi Tallapudi
Connect Via

Vamsi Tallapudi

Founder and Author at Code Refer
Android Dev, Tech Enthusiast and Blogger. Ever since my first basic android app has reached lakhs of users, I've found myself in love with Android Development :). While my primary passion is coding, during my free time, you can find me blogging, travelling or on Facebook 🙂
Vamsi Tallapudi
Connect Via

Related Posts

Vamsi Tallapudi

Android Dev, Tech Enthusiast and Blogger. Ever since my first basic android app has reached lakhs of users, I've found myself in love with Android Development :). While my primary passion is coding, during my free time, you can find me blogging, travelling or on Facebook :)

You may also like...