Android Development

Google's android guide Home Contact

Android Activities

Activity classes in Java or Kotlin display the visual UI, allow user interactions, and manage the activity's lifecycle. Activities are fundamental organizational elements in Android app development. They are responsible for the flow of the app, and allow user interaction with buttons, text fields, and other UI elements.

Activity Lifecycle

Activities have a well-defined lifecycle that consists of multiple stages, managed by lifecycle methods. Proper use of the lifecycle is crucial for managing the app's behavior, especially during transitions between states such as when the app is paused, resumed, or destroyed.

  • onCreate(): Called when the activity is first created. Initialize variables, set up the UI, and perform one-time setup tasks. Use the savedInstanceState parameter to restore data if the activity was previously destroyed and recreated (e.g., during a configuration change).
  • onStart(): Called when the activity is about to become visible but is not yet interactive. The activity is transitioning from invisible to visible.
  • onResume(): Called when the activity is fully visible and interactive, ready for user interaction. Start animations, play music, and acquire resources here, to be released in onPause().
  • onPause(): Called when the activity is about to lose focus and go into the background. Stop animations, release resources, and save data as necessary.
  • onStop(): Called when the activity is no longer visible. This happens when another activity is launched or when the user navigates away from the current activity.
  • onRestart(): Called when the activity is restarting after being stopped. It is followed by onStart().
  • onDestroy(): Called when the activity is about to be destroyed. This happens when the user explicitly finishes the activity or the system needs to free up resources.

Lifecycle Sequence Example for Transition Between Activities

A typical lifecycle sequence when transitioning between Activities:

  • When Activity A is started, its onCreate() -> onStart() -> onResume() methods are called.
  • When Activity B is started (via startActivity()), Activity A goes through onPause() -> onStop().
  • When returning from Activity B to Activity A, Activity A goes through onRestart() -> onStart() -> onResume().

Communication Between Activities

Intents: Intents are a core component of Android that allow communication and data exchange between activities, services, broadcast receivers, and other app components.

  • Explicit Intents: These specify the exact component (activity) to launch, typically within the same app. Use them to pass data between activities.
  •         Intent intent = new Intent(this, SecondActivity.class);
            intent.putExtra("key", "data to Second Activity!");
            startActivity(intent);
            
            // In the receiving activity (SecondActivity)
            Intent intent = getIntent();
            String data = intent.getStringExtra("key");
          
  • Implicit Intents: These allow the system to choose the appropriate activity to handle the intent based on actions like sending emails or opening URLs.
  •         // Sending data using an implicit intent.
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "data from app!");
          
  • Activity Result API: The new API replaces startActivityForResult() and allows handling activity results in a type-safe manner. It also supports non-activity components like Fragments and Composables.
Result Codes:

When sending data between activities and receiving a response, use result codes to indicate success or failure:

  • RESULT_OK: Operation was successful.
  • RESULT_CANCELED: Operation was canceled or failed.
Objects:

If sending objects, they must implement either the Serializable or Parcelable interface. Parcelable is recommended for performance, especially on Android.

Sending Data Between Regular Classes and Activities

From Regular Class to Activity: You can directly create an instance of the regular class inside the activity to send data. Alternatively, use callbacks or interfaces for a decoupled approach.

From Activity to Regular Class: Pass data using callbacks, method parameters, or constructors. Another approach is using a singleton object for global access or leveraging LiveData for real-time data sharing.

Activity Back Stack

The back stack organizes activities in a stack (LIFO). Pressing the "Back" button pops the top activity off the stack, returning to the previous activity. You can control the back stack behavior using Intent flags like FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK.

Activity Launch Modes

Launch modes, specified in the AndroidManifest, control how activities are created and managed within the stack. Modes like "standard," "singleTop," "singleTask," and "singleInstance" determine how instances of an activity are handled.