Intro

In a previous post we got our self’s introduced to the concept of activities in android. As we said the Activity is one of the four basic app components in Android, it is the entry point for interacting with the user. It represents a single screen with a user interface. We created an exercise to show the lifecycle of an activity. But most applications today contain multiple screens, which means they are compiled from multiple activities. Typically, one activity in an app is specified as the main activity that is the first screen to appear when the user launches the app. As we said, each activity can then start another activity in order to perform different actions. For example, the main activity in a simple note taking app may provide the screen that shows all of the current notes. From there, the main activity might launch other activities that provide screens for tasks like taking a new note and opening individual notes.

The Exercise

The point of this exercise is to see in action switching between two simple activities. In this exercise we will create two simple activities, they will each have a single button with a single purpose to take us from one activity to the other. We will create a project and the left side of your Android studio in the project view that is set to android, in the app folder you will find the java folder and in the folder named as your package ( the package that contains your main activity java file, but not the test ones) you will create a new Activity, select Empty Activity. Like this:

First we need so st the layout for the activities. In activity_main.xml we will add a button that we will name “Go to the second activity” and give it an id “nextButton”. Here is the XML code for this button:

This is how it looks like:

Then in activity_second.xml we will do a very similar creation of a button that we will name “Go back to the main activity” and give it an id “backButton”. Here is the xml code for this button:

This is how it looks like:

Now that we are done with the layout it is time for some programming. We need to create a method that when the button is clicked will take us to the second activity.  In the MainActivity we will create a method called onClickNextButton, where we will initialize a button and then use the findViewById method to set it to the nextButton that we have created. After that we need to create a listener so we can handle when the button is pressed. For this button to take us to the second activity when pressed we need to call the startActivity method and pass a new Intent into it to connect our two activities together. The intent will take two arguments. With the first argument we want to tell the intent where is our starting point, so in this exercise it is our MainActivity with this keyword, and for the second argument we pass the name of our second activity with the dot class extension, because the intent accepts only class objects for the second parameter. Here is the code:

 

Now we will call this onClickNextButton method in the onCreate method in the MainActivity and the button when clicked will take us to the second activity.

Now we will use almost the same approach in setting the second button in the second activity to take us back to the first activity when pressed. In the SecondActivity we will create a method called onClickBackButton, where we will initialize a button and then use the findViewById method to set it to the backButton that we have created. After that we need to create a listener so we can handle when the button is pressed. The difference here will be that instead of calling the startActivity method, we will call the finish method which will end the current activity and with that it will take us back to the main activity. We are using the finish method, so if someone is pressing the androids built-in back button it will work as intended and take you back to the main activity, and we wont be stacked going forward and backward between the activities. We will then call this onClickBackButton method in the onCreate method in the SecondActivity . Here is the code:

Now when we click on the “Go to the second activity” button it takes us to the second activity, and when we click on the Go back to the main activity button or the android built-in back button we go back to the main activity.

You can find the whole project on GitHub: AleksandarGulevski/SwitchBetweenActivitiesExercise

Spread the love

Leave a Reply

Your email address will not be published.