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

  1. Key States and Methods

    • onCreate():

      • Called when the Activity is first created.

      • Used to initialize essential components (e.g., UI elements, variables).

    • onStart():

      • Called after onCreate().

      • The Activity becomes visible to the user but isn’t interactive yet.

    • onResume():

      • Called after onStart().

      • The Activity is now in the foreground and fully interactive.

      • This is where you handle any tasks that require user interaction.

    • onPause():

      • Called when the Activity is no longer in the foreground (e.g., another Activity is launched or the user presses the home button).

      • The Activity is still partially visible but not interactive.

      • Use this method to save important data or stop ongoing tasks.

    • onStop():

      • Called when the Activity is no longer visible to the user.

      • The Activity is either paused or destroyed.

    • onDestroy():

      • Called when the Activity is being destroyed (e.g., the user exits the app or the system kills the process due to low memory).

  2. Key Events

    • User Navigates to the Activity:

      • Triggers onCreate(), onStart(), and onResume() if the Activity is 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 Activity pauses (onPause()) and stops (onStop()) if it’s no longer visible.

    • User Navigates Back to the Activity:

      • The Activity resumes (onRestart(), onStart(), onResume()).

  3. Key Transitions

    • Activity Launched:

      • When the Activity is started for the first time, it goes through onCreate(), onStart(), and onResume().

    • User Navigates to Another Activity:

      • If the user navigates to another Activity, the current Activity goes into the background:

        • It calls onPause() (loses focus).

        • If the user returns, it resumes with onResume().

    • Another Activity Comes into the Foreground:

      • If another Activity takes over, the current Activity calls onPause() and then onStop() if it’s no longer visible.

    • App Process Killed:

      • If the system needs more memory, it may kill the app process, leading to the Activity being destroyed (onDestroy()).

    • User Returns to the Activity:

      • If the user navigates back to the Activity, it resumes with onRestart(), followed by onStart() and onResume().


📌Tracing Activities

This script helps identify which activity is currently in the foreground (active activity).

Java.perform(() => {
    let ActivityClass = Java.use("android.app.Activity");
    ActivityClass.onResume.implementation = function() {
        console.log("Activity resumed:", this.getClass().getName());
        // Call original onResume method
        this.onResume();
    }
})

📌Tracing Fragments

This script helps identify which fragment is currently in the foreground

Java.perform(() => {
    let FragmentClass = Java.use("androidx.fragment.app.Fragment");
    FragmentClass.onResume.implementation = function() {
        console.log("Fragment resumed:", this.getClass().getName());
        // Call original onResume method
        this.onResume();
    }
})

Last updated