Blog Infos
In this article, we will discuss some new improvements to the intent filter
in Android 15
Major components
A UriRelativeFilterGroup contains a set of UriRelativeFilter objects that form a set of Intent
matching rules that must be satisfied — [URL fragments, query params, etc.]
We can define these new rules in AndroidManifest file with the new
<uri-relative-filter-group> tag
- With this tag, we can use
data tags with existing attributes as well as the new [
android:fragment, android:query, android:allow]
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
<data android:host="developer.android.com" />
<uri-relative-filter-group android:allow="true">
<data android:pathPrefix="/samples" />
<data android:query="language=kotlin" />
</uri-relative-filter-group>
<uri-relative-filter-group android:allow="false">
<data android:pathPrefix="/reference" />
<data android:fragment="jetpack" />
</uri-relative-filter-group></intent-filter>
Implementation
In the AndroidManifest file, add intent-filter to the Activity tag to handle deep links 🔗
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Android15Samples"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.Android15Samples">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
<data android:host="developer.android.com" />
<uri-relative-filter-group android:allow="true">
<data android:pathPrefix="/samples" />
<data android:query="language=kotlin" />
</uri-relative-filter-group>
<uri-relative-filter-group android:allow="false">
<data android:pathPrefix="/reference" />
<data android:fragment="jetpack" />
</uri-relative-filter-group>
<!-- https://developer.android.com/reference#jetpack-->
</intent-filter>
</activity>
</application>
</manifest>
Job Offers
Activity that shows the
Uri from the deep link 🔗 intent
class MainActivity : ComponentActivity() {
private var uri = mutableStateOf("")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
Android15SamplesTheme {
Content(modifier = Modifier.fillMaxSize(), uri = uri)
}
}
handleIntent(intent)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIntent(intent)
}
private fun handleIntent(intent: Intent) {
val uri = intent.data
this.uri.value = uri?.toString().orEmpty()
}
}
@Composable
fun Content(uri: State<String>, modifier: Modifier = Modifier) {
Scaffold(modifier = modifier) { innerPadding ->
Column(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
painter = painterResource(id = R.drawable.img_android_15_logo),
modifier = Modifier.size(200.dp),
contentDescription = "Android15 Logo"
)
Spacer(modifier = Modifier.size(32.dp))
Android15ContentHeader(
modifier = Modifier.padding(innerPadding)
)
if (uri.value.isNotEmpty()) {
Spacer(modifier = Modifier.size(32.dp))
UriInfoFromLink(
uri = uri.value,
modifier = Modifier.padding(innerPadding)
)
}
}
}
}
@Composable
fun Android15ContentHeader(modifier: Modifier = Modifier) {
Text(
text = "Android15 - Samples",
modifier = modifier,
style = MaterialTheme.typography.headlineMedium
)
}
@Composable
fun UriInfoFromLink(uri: String, modifier: Modifier = Modifier) {
Text(
text = "Uri from intent: $uri",
modifier = modifier,
style = MaterialTheme.typography.labelLarge
)
}
UI — Composable Preview
Demo
android:query
tag andandroid:allow set to
true
<!-- https://developer.android.com/samples?language=kotlin-->
<uri-relative-filter-group android:allow="true">
<data android:pathPrefix="/samples" />
<data android:query="language=kotlin" />
</uri-relative-filter-group>
android:fragment
tag andandroid:allow set to
false
<!-- https://developer.android.com/reference#jetpack-->
<uri-relative-filter-group android:allow="false">
<data android:pathPrefix="/reference" />
<data android:fragment="jetpack" />
</uri-relative-filter-group>
Stay in touch
https://github.com/navczydev?source=post_page—–735cbf39b2a7——————————–
https://twitter.com/navczydev?source=post_page—–735cbf39b2a7——————————–
References
https://issuetracker.google.com/issues/350442780?source=post_page—–735cbf39b2a7——————————–
This article is previously published on proandroiddev.com