Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

What is Context in Android?

In Android development, the term Context is fundamental and is used extensively. It's a reference to the state of the app, and provides resources, themes, and other pieces of environment information. Essentially, it's a bridge to the underlying Android system.

Key Roles of Context:

  1. Resource Access: It allows access to raw assets and resources, such as strings, drawables, and styles.
  2. Launching Activities: To start a new activity, you need a Context.
  3. Layout Inflation: Converting an XML layout file into a live view hierarchy is done via the Context.
  4. Event Broadcasts & Receivers: To broadcast an event or register a broadcast receiver, you need a Context.
  5. Accessing System Services: For services like LAYOUT_INFLATER_SERVICE, NOTIFICATION_SERVICE, and others, you need a Context to get hold of them.

Different Types of Context:

  1. Application Context: This is an instance of the entire application life cycle and is independent of the activity life cycle. It's suitable for operations that aren't associated with a UI, like setting up singletons, long-lived listeners, or services.

    Usage:

    getApplicationContext();
    
  2. Activity Context: Each Activity in an app has its own context, which is a subclass of Context. This context is tied to the lifecycle of that activity. It is also often used to load UI resources and start new activities.

    Usage:

    MyActivity.this
    // or simply:
    this // when inside an activity
    
  3. Service Context: Like Activity, a Service in Android also extends Context, so it has its own context. It's tied to the lifecycle of that service.

  4. Broadcast Receiver Context: A BroadcastReceiver also receives a Context when its onReceive method is called. This context is only valid for the duration of onReceive execution.

  5. View Context: Every View in Android also has a reference to the Context that was used to create it.

Important Considerations:

  1. Memory Leaks: One of the common pitfalls in Android is leaking a Context. This can happen when you keep a long-lived reference to an Activity or a View. This is why, for long-lived objects, it's usually better to use the ApplicationContext which is tied to the application lifecycle and won't cause individual activities to be retained.

  2. Styling and Theming: Be cautious about using the ApplicationContext to inflate views or access UI-related resources. The application context doesn't carry the theme of the current activity. Hence, inflating views using the application context can lead to styling issues.

In summary, the Context in Android is a powerful and versatile concept, but it's essential to understand its different types and their lifecycles to use it effectively and avoid potential issues.

  1. Using Context in Android Programming:

    • Access resources, preferences, and system services using the Context object.
    • Example:
      String appName = context.getString(R.string.app_name);
      
  2. Context and Resource Access in Android:

    • Access resources like strings, drawables, and layouts using the Context object.
    • Example:
      int drawableId = context.getResources().getDrawable(R.drawable.icon);
      
  3. Context and Service Binding in Android:

    • Use Context to bind to services, allowing communication between components.
    • Example:
      Intent intent = new Intent(context, MyService.class);
      context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);