Intnet
Intnet
**Explicit Intent**:
- This is when you specify the target component (activity) explicitly.
- In this example, Activity A explicitly starts Activity B.
2. **Implicit Intent**:
- This is when you don't specify the target component. Instead, you declare an action to
perform, and the system matches your request with an appropriate component.
- In this example, Activity A sends an implicit intent to view a webpage.
```java
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button explicitButton = findViewById(R.id.explicitButton);
Button implicitButton = findViewById(R.id.implicitButton);
explicitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Explicit Intent
Intent explicitIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(explicitIntent);
}
});
implicitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Implicit Intent
Uri webpage = Uri.parse("https://www.example.com");
Intent implicitIntent = new Intent(Intent.ACTION_VIEW, webpage);
startActivity(implicitIntent);
}
});
}
}
```
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
```
### activity_main.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/explicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explicit Intent"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/implicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implicit Intent"
android:layout_below="@id/explicitButton"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
```
### activity_second.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity B"
android:textSize="24sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
```
This code will create two buttons in the MainActivity. One button demonstrates an explicit intent
by opening the SecondActivity, and the other demonstrates an implicit intent by opening a
webpage in a browser.