If you are a beginner in Android development, the first thing you wish to do is to create two screens and try to switch from one screen to another. In this tutorial, you will learn how to create a basic Android application and move from one screen to another.
[alert type=white ]Latest Android Tutorials
Android Navigation Drawer Example
Android start activity once notification clicked
Force 2G/3G/LTE only mode in your Android Smartphone
Some of you make have known that the basic component of an Android application is Activity. And it can be considered as the screen visible to the user. Now let’s create two activities and switch from one activity to another when a button is pressed. The graphic which depicts the process is given below.
In Android Studio select File -> New -> New Project.
In the following screen enter a name for your App and a package name.
App Name: Activities
In the following screens select your minimum API level and then Empty Activity.
Then enter the Activity Class name and a name for the XML layout.
Overall I have Created a new project with package name net.panayiotisgeorgiou.activities, Activity name MainActivity, and layout name activity_main.
Finally select finish, Gradle will build your project.
Now you have activity_main.xml in your layouts resource directory. Let’s add a button to that layout.
Creating Profile Activity
To the left of Android Studio, you will find Project Explorer. Right Click on the app from the project explorer and select,
New -> Activity -> Empty Activity
Enter a name for Activity and layout and select finish. Here I have entered ProfileActivity and activity_profile.
Let’s add some text to identify it as another Activity. We have added a TextView which displays the text “Hello User”. Also, we added a Button which helps to go back to our Main Activity.
Launching Profile Activity
Open MainActivity.java file and create a method which launches the Activity. Here I have created a method launchActivity() which has Intent defined and startActivity() method is called to launch Activity.
Now we need to call this launchActivity() method when the button is pressed, For that we need to attach an OnClickListener to that Button. It can be done by using the setOnClickListener() method. In this, we call the launchActivity() method.
Going back to Main Activity
Now you want to Go back to Main Activity. Let’s do this. Similarly, attach an OnClickListener to the Go Back button. Call the finish() method. The finish() is an Activity method which destroys the Profile Activity and we will be back in Main Activity.
Defining Activity in Manifest
As we created new Activity using the wizard, it should be automatically defined in Android Manifest file. If not add the Activity.
Consider Activities as a Stack. Initially, Main Activity is present which is at the top of the stack. When we launch Profile Activity the Main Activity goes to the bottom of the stack and Profile Activity comes to the top. When we call finish() the Activity is popped out of the stack and Main Activity comes back to its original position. The graphic given below depicts the Stack.
[alert type=white ]Happy coding! 🙂 Check out the source code on my github account[/alert]
Follow me on Instagram
Follow me on Twitter
Follow me on Github
Subscribe to my Youtube Channel
[…] Android Switching Between Activities – Example […]