Codementor Events

Understanding "this" In Android.

Published Apr 19, 2020
Understanding "this" In Android.

What is "this" ?

It is a keyword that refers to the current object in a method or constructor. It is used both in Java and Kotlin. The most common use of the "this" keyword is to eliminate the confusion between class attributes and parameters with the same name as a property in a class. This can either be in a constructor or in a method contained in the class. For example.

public class Planet {
  String planetName;

  // This keyword used in a constructor
  public Planet(String givenPlanetName) {
    this.planetName = givenPlanetName;
  }
  
  // This keyword used in a method
  public void changePlanetName(String newPlanetName) {
    this.planetName = newPlanetName;
  }

  // The program starts here
  public static void main(String[] args) {
    Planet myPlanet = new Planet("Earth");
    System.out.println("Welcome to Planet " + myPlanet.planetName);
    
    // Changing the planet's name
    myPlanet.changePlanetName("Terra");
    System.out.println("Welcome to Planet " + myPlanet.planetName);
  }
}

NB: You can run this code here

As an android developer you must have come across the very popular Toast Object.

// Show a simple toast
Toast.makeText(this, "Hello World", Toast.LENGTH_SHORT).show();

The code above implies that "this", can be passed as an argument to a function. By inspecting the declaration of the Toast object in android, I found that the type for the "this" argument is of a type "Context". Another common usage is when we want to load another activity. For example.

// Load the login activity screen
startActivity(new Intent(this,LoginActivity.class));

Contex in android

From the official google documentation, Context is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Lets try to understand what the the Toast does. We are asking the Toast object to show a toast message, and for Toast to show the message, we have to provide it with three things, then we call the show function to display the message, which is then handled by the OS. The three things are:

  1. A form of "context" i.e an implemntation of the Context class (For specifying the app state).
  2. The message we want to display.
  3. The duration of the message.

Contex Switch in Operating Systems

According to wikipedia context switch is the process of storing the context/state of a process or thread, so that it can be restored and resume execution at a later point. This allows multiple processes to share a single CPU, and is an essential feature of a multitasking operating system.

From the definition above, context can be referred to as the state of a process in memory. As we would expect, working with classes involves changing the state at various time either by instantiating a new class or calling a method in the class.

Conclusion

For some reason people new to Android (and even people not so new) seem to completely forget about object-oriented programming, so they cram codes from tutorials, and don't understand basic principles of how things. The "this" keyword is very powerful because it holds references to state/context and if not propetly used, it can lead to serious memory leaks in android. One last thing, always remember that programming is majorly about object creation and manipulaion, for objects to be modified in memory, proper contexts has to be specified.

Discover and read more posts from Oluwabukunmi Aluko
get started
post commentsBe the first to share your opinion
Show more replies