How to Get Activity Name in Android Using ADB

Every Android developer and tester needs to know what is happening on a device. You might be debugging a tricky bug or writing a script to automate a task. In these moments, you often need to find out exactly which screen, or activity, is currently open on your phone or tablet. The most powerful way to get this information is by using a computer and a tool called ADB. This guide will show you how to get the activity name in Android using ADB, taking you from the basic command to advanced troubleshooting and automation.

The Direct Command for the Current Activity

To get the name of the activity currently on your screen, connect your device to a computer and run this command in your terminal or command prompt.

adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'

This command asks the Android system’s window manager what is in focus right now. It then filters the large amount of output to show you only the most important line. The result will look something like this.

mCurrentFocus=Window{... com.example.myapp/com.example.myapp.MainActivity}

The part after the slash is what you need. In this example, the current activity name is com.example.myapp.MainActivity. The text before the slash is the app’s package name. This method works reliably for the app you are actively using.

Preparing Your Tools and Device

You cannot run any ADB command without the right setup. This is a common first hurdle that many guides skip. First, you need the Android platform tools on your computer. You can download them from the official Android developer website. Look for a package called “SDK Platform-Tools” for your operating system.

After downloading, extract the files to a folder you can easily find. Next, you must prepare your Android device. You need to unlock a hidden menu called Developer Options. To do this, go to your phone’s Settings, then About Phone, and tap on “Build Number” seven times.

Go back to the main Settings menu, and you will now see “Developer Options.” Open it and find the setting named “USB Debugging.” Turn this setting on. This permission allows your computer to talk to your device for debugging. Now, connect your phone to the computer with a USB cable.

A prompt will appear on your phone asking you to allow USB debugging from this computer. Check the box that says “Always allow” and tap OK. Finally, open a terminal window on your computer and navigate to the folder where you extracted the platform tools. Run the command adb devices.

If you see your device listed, you are ready. If nothing appears, check your cable, the USB debugging setting, and try authorizing the prompt on your phone again. This setup only needs to be done once for each computer and device pair.

See also  How To Close An App In Android: A Simple Guide For Users

Verifying Your ADB Connection

Seeing your device in the list from adb devices is the final check. The output should show something like “List of devices attached” followed by a string of letters and numbers. This string is your device’s serial number. If you see the word “unauthorized” next to it, you need to tap “Allow” on the prompt that appears on your phone’s screen.

How Android Manages Activities and Windows

To really understand the commands, you need to know what they are asking the system. Android uses a system service called the window manager to control what is drawn on the screen. Another service, simply called the activity service, manages the lifecycle of all the app screens. When you run dumpsys window windows, you are asking the window manager to tell you its entire state.

The output is very long and technical. That is why we use the grep command to search for lines containing “mCurrentFocus” or “mFocusedApp.” These specific entries tell the system which window is currently receiving user touch inputs and which app is considered active. It is the most authoritative source for what you see on the display at any moment.

Sometimes, you need more context than just the top activity. You might want to see all the activities in the background, like a stack of cards. This is called the activity back stack. You can view this entire stack with a different command that queries the activity service directly.

Viewing the Full Activity Stack

Run the command adb shell dumpsys activity activities. This will produce a massive amount of information. It shows every running task and every activity in those tasks, along with their state. It is incredibly useful for understanding complex navigation flows or finding bugs where the wrong activity is in the stack.

Look for sections labeled “Running activities” or “Hist #0” in the output. These sections list activities in order. The activity at the top of the list is usually the one you see. This command gives you the complete picture of your app’s navigation state.

Other Useful ADB Commands and Filters

Different situations call for different tools. The basic focus command is perfect for most cases, but sometimes you need a different approach. For example, you might want to see what apps have been used recently. The command adb shell dumpsys activity recents shows the list of recent tasks.

You can filter this to just see the activity names with a command like adb shell dumpsys activity recents | grep 'intent={'. This pulls out lines that contain the launch intent for each recent task, which includes the activity name.

On newer versions of Android, a simpler command sometimes works well. Try adb shell dumpsys activity top. This command often gives a cleaner summary of just the top-most activity without all the extra window manager details. It is a good alternative if the main command gives you messy output.

See also  Fixing An Android Phone That Keeps Restarting

If you only need the package name and not the full activity, you can adjust the filter. A command like adb shell dumpsys window windows | grep -oE 'com\.\w+\.\w+' | head -1 uses a pattern to extract just the package name from the output. This can be handy for scripts that just need to know which app is open.

Fixing Common Problems and Permission Errors

You followed the steps but got a blank output or an error. Do not worry, this is common. First, make sure your device’s screen is unlocked and the app you want to inspect is open and visible. Some system screens, like the lock screen or power menu, do not report a normal activity.

The most confusing issue involves permissions. The dumpsys tool requires a special system permission called android.permission.DUMP. On normal developer devices and emulators, the ADB shell is automatically granted this permission. This is why the commands usually work.

However, some manufacturer-customized versions of Android or production devices with debugging enabled might restrict this. If a command returns very little information or fails, it is likely due to this permission. The good news is that the mCurrentFocus command from the window manager usually works even when more detailed activity commands are restricted. You do not need root access for any of these commands on a debuggable device.

When Grep is Not Available

The grep command is a standard tool on Unix-like systems, including the Android shell. However, some minimal Android shells or Windows command prompts do not have it. On a Windows computer, you can use findstr instead.

Your command would look like this: adb shell dumpsys window windows | findstr mCurrentFocus. If you are on the device itself in a terminal app and grep is missing, you might have to look through the full output manually. You can run just adb shell dumpsys window windows and scroll until you find the mCurrentFocus line.

Using the Activity Name in Automation Scripts

Finding the activity name is rarely the end goal. It is usually one step in a larger process, like an automated test. You can capture the output of the ADB command into a variable in a script. Then, your script can make decisions based on which activity is open.

For example, a bash script could run the command, save the result, and then use an “if” statement to check if the expected screen loaded. This allows you to create robust automated tests that verify app navigation. You can also use this technique in continuous integration systems to test builds automatically.

See also  How To Make A Wifi Call Android: Step-By-Step Guide

As mentioned in some community forums, apps like Tasker can use ADB permissions to read this information directly on the device. With a one-time ADB command to grant permissions, Tasker can check the current activity and trigger automations on your phone itself, without a computer. This opens up powerful possibilities for personal device automation based on what app you are using.

Frequently Asked Questions

How do I find the launchable activity for any app?

Use the command adb shell cmd package resolve-activity --brief [PACKAGE_NAME] | tail -n 1. Replace [PACKAGE_NAME] with the app’s package, like com.whatsapp. This asks the package manager for the main activity declared in the app’s manifest, which is the one that opens when you tap the app icon.

Can I get the activity name over WiFi without USB?

Yes. First, connect your device with a USB cable and run adb tcpip 5555. This restarts the ADB daemon on your phone in TCP/IP mode. After that, disconnect the USB. Find your device’s IP address in the WiFi settings.

Then on your computer, run adb connect [DEVICE_IP_ADDRESS]. Now you can run all activity commands wirelessly. This is very useful for testing on devices that are not easily reachable.

What if the command returns a system app name?

This is normal. If your home screen is visible, the mCurrentFocus will show your launcher’s activity, like com.android.launcher3.Launcher. If a system dialog is open, it will show that system package. The command reports exactly what is visually on top, even if it is not your app.

Is there a way to watch activity changes live?

You can create a simple loop in your shell. On Linux or Mac, use: while true; do adb shell dumpsys window windows | grep -E 'mCurrentFocus'; sleep 1; done. This will print the current activity every second, allowing you to see changes in real time as you navigate your app. Press Ctrl+C to stop the loop.

Why does the full activities command give so much output?

The dumpsys activity activities command dumps the state of the entire activity manager service. This includes every single task, every activity, their order, their size, and their lifecycle state. It is designed to give a complete picture for deep debugging. For just the top activity name, the window focus command is always simpler and faster.

Mastering these ADB commands gives you a clear window into what your app is doing at any moment. You can move from simply copying a command to understanding why it works and how to adapt it when things go wrong. This skill is fundamental for effective Android development, testing, and automation, making you proficient in how to get the activity name in Android using ADB.

Leave a Comment