Android页面路由,Intent隐式跳转方案

应用场景

  • 由后端传入一个String path,端上接收到该path并打开指定的页面或者web页
  • 端上页面间的跳转

Intent隐式跳转

  • MainActivity中跳转到SearchActivity
    • 传入的path为
      test://app/search?from=main&id=123
      对应在Manifest的配置
      android:host=“app” //包名
      android:path="/search" //页面名
      android:scheme=“test” //项目名
		binding.btTest.setOnClickListener {
            RouterUtil.routeByUri(
                this,
                "test://app/search?from=main&id=123"
            )
        }
  • SearchActivity中接收上个页面的参数
    private fun initData() {
        val from = intent.data?.getQueryParameter("from")
        val id = intent.data?.getQueryParameter("id")
        Log.d(TAG, "initData: from=$from,id=$id")
    }
D/SearchActivity: initData: from=main,id=123

Manifest中配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zhangyu.myapplication">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication">

        <activity android:name=".SearchActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="app"
                    android:path="/search"
                    android:scheme="test" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:host="app"
                    android:path="/main"
                    android:scheme="test" />
            </intent-filter>
        </activity>

    </application>

</manifest>

路由

package com.zhangyu.myapplication

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log


object RouterUtil {
    private const val TAG = "RouterUtil"

    fun routeByUri(context: Context, uri: String) {
        Log.d(TAG, "routeByUri: $uri")
        if (uri.startsWith("test://")) {
            Log.d(TAG, "routeByUri: 打开Android页面")
            // 页面路由
            val intent = Intent()
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            intent.action = Intent.ACTION_VIEW
            intent.data = Uri.parse(uri)
            context.startActivity(intent)
        } else {
            Log.d(TAG, "routeByUri: 打开网页")
            // WebView路由
            WebViewActivity.open(context, uri)
        }
    }
}

参考资料

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值