Lessons: 8Length: 44 minutes

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.3 Navigating Between Activities

Each screen in an app is an Activity. Your app might have a number of Activities, so users will need a way to get from one to another. In this lesson, you will learn how to navigate between the Activities in your Android app.

2.3 Navigating Between Activities

In this lesson, we will be learning about navigating between activities. I'll show you an example and then we will jump to Android Studio and you'll see the demo. So let's get started. Presenting the example, this is my contacts activity. Let's consider it to be the first activity. On hitting the button to add new contacts, I'm taken to this next activity, which we will consider to be the second activity. And again, we do this using Intent. So this was a very basic example of how you navigate between the activities in your Android application in your day to day apps. Let us move to Android Studio and see how we can implement this in our own application. Now we already have these two buttons on which we have implemented the listener. Let us add another button here, which will take us to the second activity and we will also create the second activity in our project. So moving to the xml file, let us first add a button, I will give its ID as button_second_activity and the text on it will be, So this is how my first activity will look. And now let us create the second activity. The empty activity, we'll name the activity as SecondActivity. The layout file name will be actvity_second. Yes, I need the layout to be created for our second activity. Hitting Finish, our second activity is ready. And this is the activity_second.xml file. Let's go to the manifest file. Here, the second activity is added, And now we are good to go. Now we want to implement the listener to this third button, so let us write the code for it. In the MainActivity.java but before that, in the activityMain.xml, we will use the onClick attribute because it is cleaner and easier, moveToSecondActivity. Alt+Enter, create the function and here we are. Now we will write the code to move to the second activity. It gives me error for Intent. Hitting Alt+Enter, the input is automatically handled. And this accepts two parameters, the context, from which we are calling the target activity and then the target activity class. So here, it is MainActivity.this. And our target activity is SecondActivity.class. Now we have our intent variable initialized. Now we will use it to start the activity. That's all we need to do to navigate between the activities on running our application. Here we have the third button to move to the second activity and yes, we have successfully moved to the second activity. Hitting back, this is our first activity. And on hitting the third button, this is our second activity. Let us give a default text here And this is how my second activity will look. Now when running the application, It is a little not clear that this is the second activity. So congratulations on moving from one activity to other from your Android application. In the next lesson, I'll show you how to share data from one activity to the other.

Back to the top