Empowering you to understand your world

How To Start A New Activity In Android

By Nicholas Brown.

In Android, apps have multiple screens called ‘activities’. When you see a new screen come up, it is launching a new activity. Launching an activity is quite simple in Android, as shown below.

To create an activity in Android, right click ‘app’ under the project tab of your app, then click New > Activity > Empty Activity. It doesn’t have to be an empty activity but it is the simplest type of activity to start with if you’re just getting started with Android app development.

Name your activity in a manner that clearly distinguishes its purpose from the other parts of your app. For this example, the activity name will be ViewProfile. Once you have created your new activity, you can create a button to launch it and then adding the following line of code to the button’s XML code block:

android:onClick="LaunchProfile"

Now go over to the Java source file of the activity your activity launcher button is in, which might be something like ‘MainActivity.java’ if you didn’t change the default name. Create a function called ‘LaunchProfile’, and inside that is where you’ll type the code required to actually launch the ‘ViewProfile’ activity:

Intent ProfileLauncher = new Intent(this, ViewProfile.class);
startActivity(ProfileLauncher);

When you click your new launch button, you should see that activity pop up. When the need arises, you can also send a message to the activity you are launching using ‘intent.PutExtra’, as shown in the Android documentation.

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published