Android Development 03: Android Fundamentals

Android Activity Lifecycles:

  • In Android, Activity lifecycle refers to the different states an Activity goes through from its creation to destruction. Understanding this lifecycle is essential to manage data, UI, and resources properly during app execution.

  • onCreate()

    • Called when the activity is first created.

    • Use it to initialize UI components, set content view, and restore saved instance state.

  • onStart()

    • Called when the activity becomes visible to the user.

    • UI is visible, but the activity may not be in the foreground yet.

  • onResume()

    • Called when the activity starts interacting with the user.

    • It's now in the foreground and the user can interact with it.

  • onPause()

    • Called when the system is about to resume another activity.

    • Save UI-related data and pause ongoing tasks like animations or video playback.

  • onStop()

    • Called when the activity is no longer visible.

    • Release resources or save data if necessary.

  • onRestart()

    • Called after onStop() if the activity is coming back to the foreground.
  • onDestroy()

    • Called before the activity is destroyed.

    • Cleanup all resources, unregister listeners, and avoid memory leaks.

onCreate()
   ↓
onStart()
   ↓
onResume()
   ↓
[Activity is running]
   ↑
onPause()
   ↓
onStop()
   ↓
onDestroy()
MethodDescription
onCreate()Called when the activity is first created. Initialize UI and data here.
onStart()Called when the activity becomes visible to the user.
onResume()Called when the activity starts interacting with the user.
onPause()Called when another activity comes in front. Pause animations, etc.
onStop()Called when the activity is no longer visible. Release heavy resources.
onRestart()Called when the activity is coming back to the foreground after onStop().
onDestroy()Final cleanup before the activity is destroyed.

these life cycle functions can be override on each other.

Function Overrride:

  • Function override (also called method overriding) is when a subclass provides its own version of a method that is already defined in its parent class.

    Imagine your parent gives you a default recipe for making tea . But you like it a bit different, so you override that recipe with your own version. You’re not creating a new dish—just changing how you make the same one.

class Animal {
    void makeSound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override // Optional, but good practice
    void makeSound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();  // Upcasting
        myDog.makeSound();         // Output: Dog barks
    }
}
  • Even though the reference is of type Animal, the Dog's version of makeSound() is executed — this is overriding in action.

  • Key Rules for Overriding:

    1. Method name and signature must match exactly.

    2. Only inherited methods can be overridden.

    3. The method in the parent class must not be private, final, or static.

    4. The overriding method can have more relaxed access (e.g., from protected to public).

  • Overloading → Same method name, but different parameters (happens in the same class).

Overriding → Same method name, same parameters, different class (child-parent relationship).

Function Overloading :

  • Function Overloading (also called method overloading) means defining multiple methods with the same name in the same class, but with different parameters.
class Calculator {

    // Overload by number of parameters
    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }

    // Overload by type of parameters
    double add(double a, double b) {
        return a + b;
    }

    // Overload by order of parameters
    String add(String a, int b) {
        return a + b;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();

        System.out.println(calc.add(2, 3));          // int + int
        System.out.println(calc.add(2, 3, 4));       // int + int + int
        System.out.println(calc.add(2.5, 3.5));      // double + double
        System.out.println(calc.add("Total: ", 5));  // string + int
    }
}
FeatureOverloadingOverriding
Where it happensSame classIn a subclass
SignatureMust differ (number/type/order)Must be exactly the same
Return typeCan differShould be same or covariant
PurposeTo provide multiple ways to use a methodTo change behavior of inherited method

Activity Stack:

In Android development, the activity stack (also called the back stack) is a way of managing the order in which activities (screens) are opened and navigated.

  • Think of the activity stack like a stack of plates:

    • The top plate is the current activity the user sees.

    • When you start a new activity, it's placed on top of the stack.

    • When the user presses the Back button, the top activity is popped off, revealing the previous one underneath.

MainActivity.kt file:

  • It is the Kotlin file representing the main screen (activity) of the app. It usually extends AppCompatActivity and serves as the entry point when the app launches.

  • imp video: to check log console and error detection: link

0
Subscribe to my newsletter

Read articles from Dibyashree Chakravarty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dibyashree Chakravarty
Dibyashree Chakravarty