Visit Sponsor

Written by 10:35 am Android

How to Launch an Activity in Android – Intent & Patterns

Launching an activity is a core building block in Android app development. Whether navigating between screens, passing data, or expecting results back, understanding how to properly launch and manage activity transitions is critical for scalable, maintainable applications.

This updated guide on javatechig.com explains modern patterns for launching activities using Intents, ActivityResult APIs, and handling data in both Kotlin and Java, aligned with official practices from Android Developers.

What Is an Activity in Android?

An Activity represents a single screen with a user interface. It’s a fundamental component of Android applications. When navigating between screens, you typically launch new activity instances.

Activities are part of the Android app lifecycle, and launching them correctly ensures smooth navigation and UI state management.

Launching an Activity with Intent

To navigate from one activity to another, use an explicit Intent.

Kotlin Example

val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)

Java Example

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);

This starts the specified activity.

Passing Data Between Activities

You can bundle key‑value pairs with the Intent.

Kotlin

val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("KEY_NAME", "value")
startActivity(intent)

Java

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("KEY_NAME", "value");
startActivity(intent);

Retrieving Data in Launched Activity

In SecondActivity:

Kotlin

val value = intent.getStringExtra("KEY_NAME")

Java

String value = getIntent().getStringExtra("KEY_NAME");

Always check for nulls when retrieving data.

Modern Pattern: Activity Result API

The older startActivityForResult() is now deprecated. Use the Activity Result API for cleaner result handling.

Kotlin Example

private val launcher = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) { result ->
    if (result.resultCode == RESULT_OK) {
        val data = result.data?.getStringExtra("RESULT_KEY")
    }
}

launcher.launch(Intent(this, SecondActivity::class.java))

Java Example

ActivityResultLauncher<Intent> launcher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    result -> {
        if (result.getResultCode() == RESULT_OK) {
            String data = result.getData().getStringExtra("RESULT_KEY");
        }
    }
);

launcher.launch(new Intent(MainActivity.this, SecondActivity.class));

This approach avoids coupling activity results to lifecycle methods manually and fits modern Android architecture.

Returning Results from Activity

In the launched activity:

Kotlin

val intent = Intent()
intent.putExtra("RESULT_KEY", "some result")
setResult(RESULT_OK, intent)
finish()

Java

Intent intent = new Intent();
intent.putExtra("RESULT_KEY", "some result");
setResult(RESULT_OK, intent);
finish();

Launching with Flags

Intent flags control task behavior:

FlagPurpose
FLAG_ACTIVITY_NEW_TASKLaunch activity in new task
FLAG_ACTIVITY_CLEAR_TOPClear above activities
FLAG_ACTIVITY_SINGLE_TOPReuse existing instance

Example:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)

Deep Linking to Activities

You can launch activities with custom URIs.

AndroidManifest

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="app" android:host="open" />
</intent-filter>

Launch

val uri = Uri.parse("app://open")
startActivity(Intent(Intent.ACTION_VIEW, uri))

This supports external links and deep navigation.

Handling Back Navigation

Android handles back navigation by default. You can override:

Kotlin

override fun onBackPressed() {
    super.onBackPressed()
}

Java

@Override
public void onBackPressed() {
    super.onBackPressed();
}

Use this to manage navigation flow when custom behaviors are needed.

Common Mistakes & Fixes

Crash on Launch

Cause: Incorrect intent class or null context
Fix: Use correct activity class and valid context.

Data NullPointerException

Cause: Missing extras
Fix: Always null-check when retrieving intent data.

Best Practices (2026 Updated)

  • Prefer the Activity Result API over deprecated methods
  • Respect lifecycle when launching activities
  • Use deep links for external navigation
  • Avoid oversized intent extras (use bundles or ViewModel for large data)
Visited 19 times, 1 visit(s) today
Close