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

Processes and Application Lifecycle in Android

Understanding processes and the application lifecycle in Android is crucial for building efficient and user-friendly applications. Let's break this down into two main sections: processes and application lifecycle.

1. Processes

In Android, every application runs in its own process and with its own instance of the Android Runtime (ART). Most apps should not need to directly interact with processes, but it's essential to understand how Android manages and prioritizes them. Android categorizes processes into different "importance levels" based on the components currently active and running in them:

  1. Foreground Process: A process hosting activities or services with which the user is actively interacting. Killing such processes would have a noticeable impact on the user experience.
  2. Visible Process: A process hosting activities that are not in the foreground (not interacting with the user) but are still visible. An example is an activity with the onPause() state.
  3. Service Process: A process hosting services that are started with startService(). These processes aren't directly visible to the user.
  4. Background Process: A process holding activities that aren't visible to the user and with no running services. These are prime candidates to be killed by the system when memory runs low.
  5. Empty Process: A process that doesn't hold any active application components. Android keeps it around as cache to improve startup times for potential future components.

Android may kill processes to reclaim resources, especially if memory is low. The likelihood of a process being killed is inversely proportional to its importance level, i.e., foreground processes are least likely to be killed, while empty processes are most likely.

2. Application Lifecycle

The application lifecycle is tied closely with the lifecycle of its components. The most commonly encountered component lifecycle is that of an Activity.

  • onCreate(): Called when the activity is first created. Initialization logic, such as UI inflation and data setup, typically occur here.

  • onStart(): The activity becomes visible to the user.

  • onResume(): The activity starts interacting with the user. At this point, the activity is in the foreground, and user input is captured.

  • onPause(): This is called when the system is about to put the activity into the background or when the activity becomes partially obscured. It's the first indication that the user is leaving the activity.

  • onStop(): The activity is no longer visible to the user. The activity might be completely covered by another activity, or the activity is being destroyed.

  • onDestroy(): The system is about to completely remove the activity from memory. This can happen because the activity is finishing or because the system is temporarily destroying it to conserve resources.

Apart from Activity, other Android components like Service, BroadcastReceiver, and ContentProvider also have their own lifecycles, but the Activity lifecycle is the most intricate and the one developers engage with most frequently.

By understanding and correctly implementing code within these lifecycle methods, developers can ensure that their apps behave predictably and maintain a consistent state across user interactions and system-initiated process kills.