AndroidTips & Tricks

Moving from one activity to another in android studio

Moving from one activity to another using Intent service

Any application you have used in your daily life consists of various pages or screens but you might not realize it. As an android developer, these pages or screens are known as Activities. For example; WhatsApp is the most popular social application for communication purpose. When you open the application, an activity appears in which you’ll see a chat list consisting of messages from different people. After that you’ll open someone’s chat box to reply them, as you tap on someone’s chat box, a new activity comes on the screen leaving the previous activity on the background. This is how we usually move from one activity to another using different apps and every app has many activities depending on its features. Now when creating an application, there should be many activities in your app. So in this article, we’ll learn about moving from one activity to another in android using some lines of codes in the easiest way.

In Android Studio, a service known as Intent is used for switching from one activity to another in your application. So first of all, open android studio and create a new project. After successful creation of the project, you’ll be given with activity_main.xml (UI design) and MainActivity.java (Coding) for the functionality of the layout by default.

Creating a new Activity:

Make sure you have selected the Project view’s Android sub view on the left side. After that expand App>>Java>>Package name and then right click on the package name, click new >> Activity >> Empty activity.

Moving from one activity to another in android

Open the main activity layout from res >> layout >> activity_main.xml and drag a text view to the screen and name it activity1 inside strings.xml file to avoid getting the error of hardcoded text. Place a button too for switching from activity 1 to activity 2. In the same way, open the newly created activity layout and drag a text view to the screen and name it activity 2.

Moving from one activity to another in androidMoving from one activity to another in android

 

Moving from one activity to another in android

Now let’s do some code for moving from one activity to another using Intent service. Open MainActivity.java and initialize a text view and a button of the main layout and connect it to the code by using their id’s through findViewById() method.

Moving from one activity to another in android

Moving from one activity to another in android

After that set on click listener on the button you made for switching by using the following code.

Moving from one activity to another in android

Now create a function outside the onCreate method and name it activity2. Set its access modifiers to the public so that it can be called anywhere in the program as shown in the figure. Now the actual works begins for moving one activity to another. Under the activity2() function, create an intent service to start an activity called activity2 and start the activity by passing the intent object (any name it could be) as a parameter. Following is the code shown in the figure.
Moving from one activity to another in android

Now build and run the app using your own device or virtual device.

Note: If you have any questions, Feel free to mention in comments…

Tags

Sarmad Nadeem

Sarmad is a Blogger, Android developer and a Freelancer who has been working in this field for many years. He'll help and guide you in any issue. Feel free to contact him.

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

shares
Close