Development of mobile applications on the Android platform begins with understanding the life cycle of components, and the key element here is Activity. This is a separate screen with an interface that the user sees on the display of his smartphone or tablet. For beginners who are just starting to master the environment Android Studio, the process of launching the first screen may seem like a confusing labyrinth of settings, configuration files and interface buttons.

However, if you understand the structure of the project and the principles of operation of the system, it becomes clear that โ€œopeningโ€ an activity means correctly setting up the entry point and launching the emulator or connecting a real device. In this material, we will analyze in detail all the stages: from creating a new project to debugging a running application, paying special attention to typical errors that developers encounter.

You will learn how the manifest file works, why access rights are needed and how to interpret system logs if the application crashes immediately after launch. We will not use boilerplate phrases, but will focus on specific technical details that will allow you to confidently manage the components of your application.

Preparing the development environment and creating a project

Before attempting to run any component, you need to make sure that the development environment itself Android Studio is configured correctly. This includes installing the latest version Android SDK, the presence of an emulator or a physical device connected via USB. Without a correctly configured environment, any attempts to compile the code will be doomed to failure even before execution begins.

When creating a new project through the wizard New Project the system automatically generates a basic structure, including the main activity. It is important to pay attention to the selected template: Empty Activity will create a minimal set of files, while Basic Activity will add a navigation bar and menu, which can complicate the initial understanding of the code for a beginner.

When generating the project, make sure that the development language is installed correctly - now the de facto standard is Kotlinalthough support Java still persists. The choice of language affects the syntax of the code inside the activity file, but does not change the way it is registered in the system.

๐Ÿ’ก

Use a stable version of Android Studio (Stable Channel), and not Beta or Canary, for training to avoid errors associated with raw software.

After completing the project setup, you will see the directory structure in the left panel. Key files are located in the folder app/src/main/java (or kotlin), where the logic code is located, and in app/src/main/res/layout, where the visual description of the interface is stored.

Registration of the Activity in AndroidManifest.xml

The most critical stage, without which the system simply does not know about the existence your screen is to edit the file AndroidManifest.xml. This file acts as an application passport, where all components, access rights and entry points are listed. If the activity is not registered here, an attempt to launch it will result in an exception ActivityNotFoundException.

Open the manifest file and find the section <application>. Inside it there should be a tag <activity>describing your class. For the main activity, which should open first when the application is launched, you need to add a special block intent-filter. It is he who tells the Android system that this activity is the entry point (LAUNCHER).

โš ๏ธ Attention: Make sure that the android:name points to the correct class path. If the class is in a subpackage, the path must be complete or start with a dot, for example .MainActivity.

An example of correct registration of the main activity is as follows:

<activity android:name=".MainActivity"

android:exported="true">

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

Pay attention to the attribute android:exported="true". In new versions of Android (starting from API level 12), its presence is mandatory for activities containing intent filters. Ignoring this requirement will result in a compilation error or application crash during installation.

โ˜‘๏ธ Checking the manifest

Done: 0 / 4

If you are creating a second or third activity, registering it in the manifest is also mandatory, but adding a block intent-filter with the LAUNCHER category is not necessary, unless you want the application to have several icons in the menu smartphone.

Setting up Launch Configuration and launch

After the code is written and the manifest is configured, the moment of launch comes. In the top toolbar Android Studio you will see the name of the current configuration file, usually it matches the name of the application. Clicking on the green โ€œPlayโ€ button initiates the build process (Gradle Build) and subsequent launch.

The first launch can take a significant amount of time, since the system needs to compile all dependencies, collect resources and install the APK file on the device. At this moment, a window Buildopens in the bottom panel, where the progress of tasks is displayed. Any error that occurs at this stage will be highlighted in red, indicating the line number.

If you have multiple devices connected or multiple emulators running, the system will prompt you to select the target device in the dialog box Select Deployment Target. Available gadgets are displayed here, indicating the Android version, screen density (DPI) and processor architecture.

  • ๐Ÿ“ฑ Physical Device: Real device connected via USB with debugging enabled.
  • ๐Ÿ–ฅ๏ธ Emulator: Virtual device created in AVD Manager.
  • โš™๏ธ Pair Devices Using Wi-Fi: Wireless debugging for Android 11 and higher.

For successful debugging on a real device, you must enable developer mode in the smartphone settings and activate item USB Debugging. Without this, the computer will not see the phone as a debugging device, and the launch button will be inactive or will display a missing devices error.

๐Ÿ“Š What device are you using for debugging?
Android Studio emulator
Real smartphone via USB
Wireless debugging Wi-Fi
Device from a colleague

Finding and eliminating startup errors

The first startup does not always go smoothly. Often developers are faced with a situation where an application is installed, but immediately closes (โ€œcrashesโ€). In this case, the main diagnostic tool becomes the window Logcat, which displays system logs in real time.

In the logs you should look for lines with the level of importance Error or Fatal. The most common cause of a crash is an attempt to access an interface element before calling a method setContentView or using an incorrect resource ID. Also common are errors NullPointerExceptionthat occur when working with uninitialized variables.

โš ๏ธ Attention: If you see an error Installation failed with message INSTALL_FAILED_TEST_ONLYin the logs, this means that the test-only flag is set in the assembly. Uncheck "Debuggable" in the build settings or use the command adb install -t.

Another common problem is SDK version mismatch. If the file build.gradle is specified minSdkVersion higher than the Android version on the connected device, installation of the application will not be possible. The system will clearly indicate this in the launch window, prompting you to select another device.

To analyze the call stack (Stack Trace), it is useful to use a filter in Logcat. Enter your package name in the search field to filter through thousands of system messages and see only logs specific to your application. This significantly speeds up the search for the root of the problem.

Running a specific activity from code

In complex applications, there is often a need to open not the main activity, but a specific screen from another place in the app or even from another application. For this, a mechanism is used Intent โ€”a message object that requests an action from the system.

To programmatically open another activity inside your application, an explicit intent is created. The constructor is passed the context of the current screen and the class of the target activity. After setting up the intent, the method is called startActivity().

val intent = Intent(this, SecondActivity::class.java)

intent.putExtra("USER_ID", 123)

startActivity(intent)

In this example, we not only open a new screen, but also transfer data to it through the mechanism putExtra. The receiving activity will be able to retrieve this data in the method onCreate through the object intent. This is the standard way to navigate between screens in the Android architecture.

What is a Task Stack?

When you open a new activity, it is placed on the back stack. Clicking the back button removes the top activity from the stack and returns the user to the previous screen. This stack can be controlled through Intent flags, for example FLAG_ACTIVITY_NEW_TASK.

If you need to open an activity from the outside (for example, via a link from a browser), an implicit Intent is used indicating the Action and Data. In this case, the system itself will find an application capable of processing such a request, based on the filters in the manifest.

Table of main life cycle methods

Understanding at what point an activity is opened is impossible without knowing its life cycle. The system calls certain callback methods in strict sequence. Knowing these steps helps to properly initialize data and free resources.

Below is a table describing the main methods that are affected when the screen is opened and displayed to the user.

Method State description Developer actions
onCreate() Activity is being created for the first time Initializing variables, calling setContentView
onStart() The activity becomes visible Preparing the resources that the user needs
onResume() The activity is active and ready for input Starting animations, resuming logic
onPause() Activity is partially blocked Saving temporary data, pausing heavy processes
onStop() Activity is completely hidden Releasing resources not needed in the background

Method onCreate is called only once during the lifetime of the activity instance, while onStart and onResume can be called repeatedly, for example, when rotating the screen or returning from the background. It is important to place the interface initialization code in onCreateto avoid duplication of operations.

Ignoring paired methods (for example, starting a timer in onResume without stopping it in onPause) often leads to memory leaks and incorrect application behavior when collapsing.

๐Ÿ’ก

Correct implementation of lifecycle methods ensures that your application will work stably when switching between tasks and will not consume battery in the background.

Frequently asked questions about launching an Activity

During the development process, students and novice engineers have many clarifying questions. We have collected the most popular of them so that you can quickly find a solution without having to look for information on third-party forums.

Why does the application crash with the error "App keeps stopping" immediately after launch?

Most often this happens due to an error in the code inside the method onCreate. Check Logcat: if it is there NullPointerException, then you are trying to find an element by ID that is not in the layout, or you forgot to call setContentView. Also check if the activity class name is correct in the manifest.

Is it possible to open an Activity without creating a new instance?

Yes, this is done using flags in the Intent. For example, the Intent.FLAG_ACTIVITY_SINGLE_TOP flag will allow you to reuse an existing activity instance if it is already on top of the stack, instead of creating a new copy. This is useful for notification screens or chats.

How to pass data from one Activity to another?

The easiest way is to use Intent.putExtra() for primitive data types and serializable objects. For complex scenarios, it is better to use architecture patterns like ViewModel or Singleton so as not to overload intents with large amounts of data.

Why does the emulator run very slowly at startup?

Slow operation of the emulator is often associated with the lack of hardware virtualization (VT-x/AMD-V) in the BIOS or lack of RAM. Try creating an emulator with an x86_64 system image instead of an ARM one, as it runs much faster on Intel and AMD processors thanks to instruction translation.

Do you need to close the Activity manually?

No, the Android system itself manages memory and lifecycle. Calling the method finish() is acceptable if you need to explicitly end the screen (for example, after logging in), but in most cases it is enough just to go to another screen, and the system itself will destroy the previous one when it needs memory.