对比Android页面直接跳转方式(为学习Arouter做准备)

对比Android页面直接跳转方式(为学习Arouter做准备)

前言

上文中我们已经学习了怎么用Arouter路由框架进行页面的跳转,但是只是用了,并没有思考为什么要用路由框架?它有什么好处吗?原生的跳转不能达到效果吗?现在我们复现下跳转时候的代码:

ARouter.getInstance().build("/app/ListPage") //页面跳转,指定跳转路径
                .withString("name", "来自主页").navigation();//增加页面跳转的参数:key为name

好了,由此看到,Arouter确实简化了跳转的参数匹配,注解功能使得我们处理数据很方便。其实,它的好处不止那么一点点,现在我们来看一下原生Android的跳转方式,后面来对一下对比。

原生Android页面跳转方式

  • 用action来跳转(隐式跳转)
    (1)action需要在AndroidManifest中进行IntentFilter声明:
   <activity android:name="com.demo.ListtActivity"
                  android:launchMode="singleTask"
                  android:clearTaskOnLaunch="true"
                  android:screenOrientation="landscape"
                  android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
                  android:noHistory="true">

            <intent-filter>
                <action android:name="com.demo.listpage"/>
                <data android:scheme="mgtvapp"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

(2)跳转方式:

  try {
            Intent intent = new Intent();
            intent.setAction("com.demo.listpage"); //action启动activity
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }catch (Exception e){
               e.printstack();
        }

说明:
如果有多个Activity匹配该action,那么就会弹出一个对话可框来提示供用户选择进入哪一个。

  • 用类名跳转
    该方式很简单,就是知道跳转组件间的类名,并且可以直接访问可见才能进行跳转。
    Intent intent = new Intent(MainActivity.this, ListActivity.class); 
     startActivity(intent);

说明:
该方式依赖性太强,必须知道目标对象且要有权限访问。

  • Uri方式跳转
    通过 进行数据匹配后进行跳转,在Android中声明data标签:
    通过scheme协议定义参数,如下所示:
    app://vod/player?Id=1234&name=hello
    参数为path后面的字符串
<intent-filter>
                <action android:name="android.intent.action.VIEW"/>
                <data android:scheme="app" android:path="/vod/player"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

页面接收到app://vod/player开头的intent数据后,在对path后的数据进行路径解析处理,分发跳转。
如打开电话页面:
Uri uri = Uri.parse(“tel:12334455”);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);

说明:
两个应用的Scheme协议相同,系统会响应先安装的应用

跳转方式对比

以上方式进行一个对比:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值