How to integrate Explicit intents with other Android components?

Explicit intents are used to start a specific component, such as an Activity or a Service, within your application or another application. They are particularly useful when you know the exact class you want to start.

In this example, we will demonstrate how to integrate explicit intents to start a new Activity and pass data between Activities.

// MainActivity.java package com.example.myapp; import android.content.Intent; import android.os.Bundle; import android.view.View; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void openSecondActivity(View view) { Intent intent = new Intent(this, SecondActivity.class); intent.putExtra("EXTRA_MESSAGE", "Hello from MainActivity!"); startActivity(intent); } } // SecondActivity.java package com.example.myapp; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = getIntent(); String message = intent.getStringExtra("EXTRA_MESSAGE"); TextView textView = findViewById(R.id.textView); textView.setText(message); } }

keywords: Android Explicit Intents Activities Integrate Intents.