Launch Activity is a fundamental action in the development, testing and deep debugging of applications under Android. Understanding how to correctly initiate an interface component allows engineers to check the logic of the application, reproduce bugs, and even open hidden system menus that are not accessible through the standard launcher. This process can be performed either programmatically inside the application code, or remotely via the command line using a tool adb.

In this article we will analyze in detail all the available methods for initializing an activity, including passing parameters and working with flags. Particular attention will be paid to diagnosing problems when the application does not respond to launch commands, as well as the specifics of working with protected system components. Knowing these nuances is critical for anyone working with Google's mobile ecosystem.

Activity architecture basics and lifecycle

Before moving on to launch commands, it is necessary to understand what it is Activity in the context of the Android architecture. This is a separate screen with a user interface, which is one of the key components of any application. The system manages the life cycle of these components by calling methods in response to user actions or system events.

When you initiate startup, the system creates an instance of the class and calls the method onCreate. It is at this moment that the interface is initialized and data is loaded. If the activity already exists in the task stack, the behavior of the system will depend on the flags and attributes passed in the manifest.

It is important to distinguish between the concepts Task and Stack. Activities are grouped into tasks, which the user sees as separate applications in the recent apps menu. Incorrect stack management at startup can lead to memory leaks or incorrect navigation behavior when the back button behaves unpredictably.

โš ๏ธ Attention: Attempting to launch an activity that is not exported in the manifest (android:exported="false") from outside the application (for example, through ADB or another application) will result in a security error. SecurityException.

For correct operation, you must take into account the current state of the application. If the application process has been killed by the system to free up memory, starting an activity will require a complete restart of the process, which takes more time and resources.

Running an Activity via the ADB command line

The most common way to initiate an action outside of application code is to use a debug bridge Android Debug Bridge. This method is indispensable for automated testing and manual playback of scripts. The command is executed through a terminal on the computer with the device connected.

The basic syntax of the command requires specifying a component in the format package/class. The system itself will determine whether to create a new process or use an existing one. To execute the command, you typically use a utility am (Activity Manager).

adb shell am start -n com.example.app/.MainActivity

If the activity class name is in the same package as the application, you can use a shorthand notation with a leading dot. However, for system components or third-party applications, always specify the full package name to avoid conflicts.

Often you need to pass data inside an activity at startup. This is implemented through flags --es (string), --ei (integer) or --ez (boolean). For example, to open a specific product in a store, you can pass its ID.

  • ๐Ÿ“ฑ -n: Specifies the name of the component (package/class) to launch explicitly.
  • ๐Ÿ“ฆ -a: Specifies an action to launch implicitly, for example, opening a link or file.
  • ๐Ÿ”ข --ei: Passes an integer value in the Intent arguments.
  • ๐Ÿ“ --es: Passes a string value in the Intent arguments.
๐Ÿ“Š Which launch method do you use most often?
Via ADB
In Java/Kotlin code
Via desktop shortcuts
I donโ€™t use

Programmatic launch in Java and Kotlin

Inside the application code, launch is carried out using an object Intent. This is a message that describes the action to be performed. In the language Kotlin the syntax is more concise, but the logic remains identical to the implementation in Java.

For explicit launch, when you know exactly the class of the target activity, the Intent constructor is used, which takes the context and class of the target. This is a standard way to navigate between screens within one application.

// Example on Kotlin

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

intent.putExtra("USER_ID", 12345)

startActivity(intent)

Implicit launch is used when the application does not know which component will process the request. For example, when opening a web page, the system will select the browser itself. Here it is important to correctly specify action and data.

When launching programmatically, the problem of loss of context often arises. If you are launching an activity from an Application Context rather than an activity, you definitely need to add a flag, otherwise the application will crash. FLAG_ACTIVITY_NEW_TASK, otherwise the application will crash.

๐Ÿ’ก

Use constants for Intent keys (for example, companion object in Kotlin) to avoid typos when passing data between activities.

Task stack management and flags launch

The behavior of the activity stack is regulated by flags passed to the Intent, or attributes in the file AndroidManifest.xml. Understanding the difference between singleTop, singleTask and singleInstance is critical to building correct navigation.

The FLAG_ACTIVITY_CLEAR_TOP flag is often used to reset the stack. If the target activity already exists on the stack, all activities on top of it will be destroyed and the existing instance will receive a new Intent via the method onNewIntent.

Flag / Mode Behavior Description Typical Usage
standard Creates a new instance every time Regular content screens
singleTop Does not create a new one if it is already on top of the stack Notification screens, chats
singleTask Creates a new task or clears the stack to this screen Main application screen
singleInstance Isolated task, only one activity Call screens, launchers

Using the flag FLAG_ACTIVITY_NEW_TASK selects the activity into a new task. This is useful for creating widgets or shortcuts that need to work independently of the main application thread.

The wrong combination of flags can cause the user to get stuck on a particular screen and not be able to go back. Always test scenarios for pressing a physical or software "Back" button after implementing new flags.

๐Ÿ’ก

SingleTask mode ensures that there is only one instance of activity in the task history, which is ideal for the main screen.

Error diagnosis and hidden activities

A common problem is when the run command is executed, but nothing happens, or A black screen appears. This may indicate that the activity is expecting certain data in Intentthat was not passed.

For debugging, use the command logcat at the same time as startup. Filtering by tag ActivityManager or the name of your package will allow you to see a detailed report on the reason for the failure to launch.

adb logcat | grep ActivityManager

Hidden activities are often used by developers to debug or test functions. They do not have an icon in the launcher, but can be called directly if they know the exact class name. However, in modern versions Android access to them is limited by security policies.

โš ๏ธ Attention: Starting with Android 10, background applications are limited in their ability to launch activities. An attempt to do this without user interaction will be blocked by the system.

If you are developing a service that needs to show an interface to the user (for example, for an alarm or a call), use special permissions or launch an activity from a high priority service, although this is not recommended without a good reason.

Running system settings and special screens

Android provides a set of standard Intent actions for opening system settings. This allows developers to redirect users to the desired section of the menu if an application requires specific permissions or settings.

For example, an action is used to open Wi-Fi settings Settings.ACTION_WIFI_SETTINGS. In the command line, this corresponds to launching the component com.android.settings/.Settings$WifiSettingsActivity (the path may differ on different firmwares).

  • ๐Ÿ”‹ Battery: android.settings.BATTERY_SAVER_SETTINGS
  • ๐Ÿ“ถ Wi-Fi: android.settings.WIFI_SETTINGS
  • ๐Ÿ“ฑ Applications: android.settings.APPLICATION_DETAILS_SETTINGS
  • ๐Ÿ”’ Security: android.settings.SECURITY_SETTINGS

Device manufacturers (Xiaomi, Samsung, Huawei) often modify standard paths to settings. What works on โ€œpureโ€ Android may not work on a custom shell. In such cases, only enumerating possible class names or using universal Actions helps.

Secret codes of engineers

Some activities can be launched by typing code in the dialer, for example ##4636## opens the phone testing menu, which is essentially launching the hidden TestingActivity activity.

โš ๏ธ Attention: System settings interfaces and activity class names may change with Android or firmware updates. Do not rely on hard-coded paths in production code.

Frequently asked questions (FAQ)

Why does it give the error "Error: Activity not started, unable to resolve Intent" when launched via ADB?

This error means that the system does not find an activity with the specified name in the specified package. Please check the correct spelling of the package and class name. Make sure that the application is installed and the activity is declared in the manifest with the attribute android:exported="true".

Is it possible to launch the activity of a minimized application?

Yes, this is possible. If the application process is alive, the activity will expand on top of the current screen. If the process was killed, the system will first restart the application, which may take a few seconds. Make sure you have rights to display on top of other windows if required.

How to pass an array of data when starting an activity?

Use the putStringArrayListExtra or putIntegerArrayListExtra methods in the Intent object. In the ADB command line, passing complex arrays is difficult and requires the use of special formats or wrapper scripts.

What is Task Affinity and how does it affect startup?

Task Affinity determines which task (stack) the activity prefers to belong to. If you launch an activity with a different affinity, it may open in a new Recent Applications window, creating the illusion of a separate app.

Is it safe to launch hidden system activities?

No, it is not recommended. Hidden activities are not intended to be used by third party applications and may change or be removed at any time. Calling them may lead to system instability or violation of Google Play security policies.