You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过Jetpack Compose Navigation从其他应用打开文件并跳转指定页面?

问题:通过外部应用打开文件时,Jetpack Compose Navigation DeepLink无法自动导航到指定页面

我已经在应用Manifest中配置了intent-filters,应用可以从其他文件浏览器/资源管理器应用中被打开,MainActivity中能正确获取到唤起的Intent信息:

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Timber.d("intent onCreate ${intent.action} ${intent.data}")
        
        // 打印内容为 "intent onCreate android.intent.action.VIEW 
        // content://com.speedsoftware.rootexplorer.fileprovider/root/storage/emulated/0/Download/test.png"
    }
}

但我希望通过其他应用打开文件时,Jetpack Compose能自动导航到AddNewFileRoute页面,以下配置并未生效:

data object AddNewFileRoute

fun NavGraphBuilder.addNewFileScreen(
    onBackClick: () -> Unit
) {
    composable<AddNewFileRoute>(
        deepLinks = listOf(
            navDeepLink {
                uriPattern = "content://" // 也尝试过 "content://*"
                mimeType = "*/*"
                action = Intent.ACTION_VIEW
            },
            navDeepLink {
                uriPattern = "file://"
                mimeType = "*/*"
                action = Intent.ACTION_VIEW
            }
        )
    ) {
        AddNewFileScreen(
            onBackClick = onBackClick
        )
    }
}

应用会被正常打开,但停留在起始页面(HomeRoute)。

使用的Navigation库版本:

androidxNavigation = "2.8.9"

如果为特定外部文件指定硬编码的完整路径,DeepLink配置可以正常生效:

uriPattern = "content://com.speedsoftware.rootexplorer.fileprovider/root/storage/emulated/0/Download/test.png"

内容的提问来源于stack exchange,提问作者user924

火山引擎 最新活动