View activity stack in Android
To display the activity stack, run the following command :
adb shell dumpsys activity activities
How to see the activity stack in debug?
1-You can use Hierarchy Viewer
within Eclipse
.You can see all connected devices and emulators and the activity stack
. And in addition in the tree view you can see much more information about the view itself:
2-From the command line
, you can use this: adb shell dumpsys activity
.This asks the activity manager to print a dump of its current state. The first part of that is the complete activity history, organized by task.
You can see more details here
.
Android | Print activity task stack/s in LogCat
What I am trying to do isn't possible out of the box using APIs within the SDK as of now. More info can be found in this link:
How to get a list of my app's tasks and the stack of their Activities?
My Solution:
I had to add logging in base class of my app, printing the activity's name with its task ID to debug the problem I'm facing. Here's the code of my base activity class:
public abstract class BaseAppActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TESTING", "CREATED: " + getClass().getSimpleName() + " -- TASK ID: " + getTaskId());
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("TESTING", "DESTROYED: " + getClass().getSimpleName() + " -- TASK ID: " + getTaskId());
}
}
How to get a list of my app's tasks and the stack of their Activities?
How can I get a list of all the Tasks for my current application
There is no such concept. Tasks can contain activities from an application. To drawn an analogy, no Web site owns the back stack of a browser tab, though a Web site's pages may be in the back stack of a browser tab.
then get the Activities on those tasks' stacks?
That is not possible, as the activities are not necessarily all from your process.
The ActivityManager class lets me query task info based on what's recent or what's running but how do I query task info just for my current app?
You don't, strictly speaking.
You can iterate over the running tasks, and you can find those where one of your activities is the baseActivity
or the topActivity
based on those ComponentName
values.
Or, your activities know their task ID, obtained via getTaskId()
. If you need to, you can find those tasks in the running tasks, matching up the ID values, to find out task-level details about the activity's task.
How to check if an activity is the last one in the activity stack for an application?
UPDATE (Jul 2015):
Since getRunningTasks() get deprecated, from API 21 it's better to follow raukodraug answer or Ed Burnette one (I would prefer second one).
There's possibility to check current tasks and their stack using ActivityManager.
So, to determine if an activity is the last one:
- request android.permission.GET_TASKS permissions in the manifest.
Use the following code:
ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);
if(taskList.get(0).numActivities == 1 &&
taskList.get(0).topActivity.getClassName().equals(this.getClass().getName())) {
Log.i(TAG, "This is last activity in the stack");
}
Please note, that above code will be valid only if You have single task. If there's possibility that number of tasks will exist for Your application - You'll need to check other taskList elements. Read more about tasks Tasks and Back Stack
Related Topics
Event Onclick for a Button in a Custom Notification
How to Open a Fragment on Button Click from a Fragment in Android
How to Change the Font Size of Tabhost in Android
Exporting SQLite Database to CSV File in Android
Authentication Using Facebook at First and Then Google Causes an Error in Firebase for Android
How to Change Text Color of Simple List Item
Oauth 2.0 Authorization for Linkedin in Android
What Is a Full Android Database Helper Class for an Existing SQLite Database
How to Scroll to Bottom in a Scrollview on Activity Startup
How to Check If Facebook Is Installed Android
Livedata Prevent Receive the Last Value When Start Observing
Httpurlconnection Worked Fine in Android 2.X But Not in 4.1: No Authentication Challenges Found
Error: Configuration with Name 'Default' Not Found in Android Studio
Sharedpreferences and Thread Safety