Development of applications for the Android mobile operating system is impossible without understanding the concept Activity. This is the fundamental building block of the interface, representing a separate screen with which the user interacts. Whether it’s the login screen, a list of products, or profile settings, each of them is a separate activity. The correct organization of these components is critical to the application architecture and ease of navigation.
In the environment Android Studio the process of adding a new screen is automated and intuitive, but beginners are often faced with configuration nuances. It's important to not just create the file, but also to properly associate it with the manifest, configure the layout, and understand the component lifecycle. Modern versions of IDEs offer many templates that speed up routine tasks, but understanding what's going on "under the hood" remains a key skill for professional growth.
This guide will detail the process of creating a new component, from choosing a template to registering it in a system file. AndroidManifest.xml. We will look at different types of activities, features of their integration into the navigation graph and typical mistakes that should be avoided at the start of a project.
Preparing the environment and understanding the architecture
Before you start generating code, you need to make sure that your project is configured correctly. Android Studio must be updated to the latest version, and project dependencies must be synchronized. Creating a new screen is not an isolated action; it affects the structure of the entire project, including layouts resources and navigation logic.
In the Android architecture, a component Activity acts as an entry point for user interaction. It controls the window in which the interface is drawn. When you launch an application, the system creates an instance of your main activity. Adding new screens requires understanding how they will be launched: explicitly through intents or implicitly through system actions.
It is worth noting that starting with certain versions of the SDK, the approach to navigation has changed. If earlier each action often required a new class, now the architecture with one activity and many is popular Fragments. However, the classic approach with multiple Activities is still widely used in legacy projects and for isolating complex functional blocks.
⚠️ Attention: Do not create an excessive number of Activities for simple tab switches. For bottom navigation or tabs, it is better to use Fragments to avoid screen flickering and loss of state when rotating the device.
Step-by-step guide for creating through the wizard
The most reliable and fastest way to add a new screen is to use the built-in IDE wizard. This tool not only generates a Java or Kotlin class, but also automatically creates a markup file, registers the component in the manifest, and configures the underlying logic. You don't have to write boilerplate code manually, which reduces the likelihood of syntax errors.
To get started, open your project in Android Studio. In the project panel (usually on the left), find the folder app, then expand the directory java (or kotlin). Right-click on the package in which you want to place the new class, and select Newfrom the context menu, and then Activity. Next, you will be offered a list of available templates.
The choice of template depends on the task. For example, Empty Activity will create the minimum required code, while Navigation Drawer Activity will immediately set up the side menu. After selecting the type, a configuration window will open where you need to set the class name, layout file name and screen title.
☑️ Check before creation
Pay special attention to the field Launcher Activity. If you check this box, the screen you create will become the entry point to the application, replacing the current home screen. For regular internal pages, this option must be cleared, otherwise, when launched, the application will not open from the place intended by the architect.
Overview of available Activity templates
The development environment offers a wide range of preset configurations. Understanding the differences between them can save you hours of manual work. Each template is optimized for a specific use case and includes specific interface and logic elements.
Let's consider the most popular options that you will find in the creation wizard:
- 📄 Empty Activity: A basic template that creates a clean screen without unnecessary elements. Ideal for custom interfaces, where you design each button and field yourself.
- 📱 Phone Window: A template that imitates a standard phone window with an ActionBar. Suitable for most standard applications.
- 🗄️ Navigation Drawer Activity: Creates a screen with a sliding side menu. Includes a complex structure of fragments and event handlers.
- ⚙️ Settings Activity: Automatically generates a settings screen based on a PreferenceFragment, making it easy to create application configuration menus.
- 📋 Scrolling Activity: Designed for scrolling content, includes
CollapsingToolbarLayoutfor beautiful header collapsing effects.
Choosing the wrong template may result in you having to delete unnecessary files and rewrite code. For example, if you want a simple list, you shouldn't take a template with a navigation drawer, as it will add unnecessary dependencies and make the code harder to maintain in the future.
AndroidManifest Configuration and Registration
After creating a class, the system will not see the new screen until it is registered in the manifest file. The wizard does this automatically by adding the element <activity> inside the tag <application>. However, manual verification of this entry is necessary, especially if you made changes to the project structure manually.
Inside the activity tag, an attribute must be specified android:namecontaining the full name of the class. Also often used is the android:labelattribute, which defines the screen title visible to the user in the top bar or in the list of recent applications. To support different languages, it is better to put the label value in resource files strings.xml.
| Attribute | Description | Required |
|---|---|---|
android:name |
Full name of the Activity class | Yes |
android:label |
Screen title for user | No (taken from the app label) |
android:theme |
Design style (light/dark theme) | No (taken from application) |
android:exported |
Availability for other applications | Yes (for Android 12+) |
Starting with Android 12 (API level 31), the attribute android:exported became mandatory. If your activity contains an intent-filter, you must explicitly indicate whether it can be launched by other applications. The value true opens access and false makes the component private for your application.
⚠️ Attention: In Android 12 and higher, the absence of the
android:exportedattribute for an activity with intent-filter will lead to a compilation error or application crash during installation. Always check this setting manually.
Navigating and launching a new screen
Creating a class is only half the battle. In order for the user to get to a new screen, it is necessary to implement a transition mechanism. In Android, the class Intentis used for this. This is a message object that tells the system that it intends to perform a certain action, in this case, opening a specific component.
For an explicit launch (when you know exactly the class of the target screen), the code looks like this. Let's say you are in MainActivity and want to go to DetailActivity by pressing a button:
val intent = Intent(this, DetailActivity::class.java)
startActivity(intent)
If you need to transfer data to a new screen, use the putExtramethod. This allows you to pass strings, numbers, serializable objects, and other types of data. On the receiving side, this data is retrieved through an object intent in a method onCreate.
What is Implicit Intent?
Implicit launch is used when you do not know the exact class, but describe an action. For example, open a link in a browser or dial a phone number. The system itself will find a suitable application to complete the task.
A modern approach to navigation recommends using the component NavGraph and the Navigation library. This allows you to visualize transitions between screens in a special editor and manage the back-stack more flexibly than when using direct calls startActivity.
Life cycle and common errors
Each activity goes through a strict life cycle consisting of methods onCreate, onStart, onResume, onPause, onStop and onDestroy. Understanding the order in which they are called is critical to resource management. For example, heavy operations should be stopped at onPauseso as not to block the interface of the next screen.
A common mistake for beginners is to try to access interface elements before calling setContentView. This will result in a fatal exception NullPointerException. First you should inflate the markup, and only then search by ID.
It is also worth remembering about configuration changes. When you rotate the screen, the default activity is destroyed and recreated. If you do not save state in a method onSaveInstanceState, all user input will be lost. For complex scenarios, it is better to use ViewModel.
Use logging in lifecycle methods (for example, Log.d("Lifecycle","onCreate")) to track the order of calls when testing the application. This will help you understand exactly when data is lost.
Another common problem is Memory Leak. If you store a reference to an activity's Context in a static variable or long-lived object, the garbage collector will not be able to free the memory even after the screen is closed. Always use ApplicationContext for global tasks.
Frequently asked questions
Is it possible to create an Activity without a layout file (XML)?
Yes, it is possible. You can programmatically create all interface elements in code using the ViewGroup and View classes and pass the root view to the setContentViewmethod. However, maintaining such code is more complex than working with XML or Jetpack Compose.
What is the difference between an Activity and a Fragment?
Activity is a separate window screen managed by the system. Fragment is a modular part of the interface that lives inside an Activity. Fragments are more flexible: they can be combined, replaced and used on tablets to display several panels at the same time.
Why does the application crash when starting a new Activity?
Most often the reason is that the new component is not registered in AndroidManifest.xml. The second popular reason is an incorrectly specified class name in the Intent or the absence of a default constructor for the Activity class.
How to pass an object between Activity?
The object must implement the interface Serializable or Parcelable. Then it is placed in the Intent through the putExtramethod. For complex objects, it is preferable to use Parcelable due to better performance.
Do I need to close the Activity manually?
No, the Android system itself manages the activity stack. Calling the method finish closes the current window and returns the user to the previous one. This should not be abused, as it may disrupt the user-expected navigation with the "Back" button.