0% found this document useful (0 votes)
9 views5 pages

Intnet

The document discusses explicit and implicit intents in Android. It provides code examples of an activity using explicit intent to start a new activity, and using implicit intent to open a webpage. It includes the code for the two activities and their layouts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Intnet

The document discusses explicit and implicit intents in Android. It provides code examples of an activity using explicit intent to start a new activity, and using implicit intent to open a webpage. It includes the code for the two activities and their layouts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

**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.

Here's the code:

### Activity A (MainActivity.java)

```java
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@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);
}
});
}
}
```

### Activity B (SecondActivity.java)


```java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

@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.

You might also like