Tracing Activities
Activities in the system are managed as activity stacks. When a new activity is started, it is usually placed on the top of the current stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits. There can be one or multiple activity stacks visible on screen.
The following diagram shows the important state paths of an activity.

📌Here’s a concise explanation
Key States and Methods
onCreate():
Called when the
Activityis first created.Used to initialize essential components (e.g., UI elements, variables).
onStart():
Called after
onCreate().The
Activitybecomes visible to the user but isn’t interactive yet.
onResume():
Called after
onStart().The
Activityis now in the foreground and fully interactive.This is where you handle any tasks that require user interaction.
onPause():
Called when the
Activityis no longer in the foreground (e.g., anotherActivityis launched or the user presses the home button).The
Activityis still partially visible but not interactive.Use this method to save important data or stop ongoing tasks.
onStop():
Called when the
Activityis no longer visible to the user.The
Activityis either paused or destroyed.
onDestroy():
Called when the
Activityis being destroyed (e.g., the user exits the app or the system kills the process due to low memory).
Key Events
User Navigates to the Activity:
Triggers
onCreate(),onStart(), andonResume()if theActivityis starting fresh.
Apps with Higher Priority Need Memory:
The system may kill the app process, leading to
onDestroy().
Another Activity Comes into the Foreground:
The current
Activitypauses (onPause()) and stops (onStop()) if it’s no longer visible.
User Navigates Back to the Activity:
The
Activityresumes (onRestart(),onStart(),onResume()).
Key Transitions
Activity Launched:
When the
Activityis started for the first time, it goes throughonCreate(),onStart(), andonResume().
User Navigates to Another Activity:
If the user navigates to another
Activity, the currentActivitygoes into the background:It calls
onPause()(loses focus).If the user returns, it resumes with
onResume().
Another Activity Comes into the Foreground:
If another
Activitytakes over, the currentActivitycallsonPause()and thenonStop()if it’s no longer visible.
App Process Killed:
If the system needs more memory, it may kill the app process, leading to the
Activitybeing destroyed (onDestroy()).
User Returns to the Activity:
If the user navigates back to the
Activity, it resumes withonRestart(), followed byonStart()andonResume().
📌Tracing Activities
This script helps identify which activity is currently in the foreground (active activity).
📌Tracing Fragments
This script helps identify which fragment is currently in the foreground
Last updated