码迷,mamicode.com
首页 > 其他好文 > 详细

ActivityRouter 框架简单实用

时间:2019-02-27 15:49:39      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:putextra   maven   sheng   .text   uid   bundle   epo   cti   zha   

ActivityRouter组件化开发小助手用法如下:

跟目录build.gradle

dependencies {
        // activityRouter
        classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘
    }

allprojects {
    repositories {
        // ActivityRouter
        maven { url "https://jitpack.io" }
    }
}

module(app)  build.gradle

dependencies{
    // ActivityRouter
    implementation ‘com.github.joyrun.ActivityRouter:router:0.6.2‘
    annotationProcessor ‘com.github.joyrun.ActivityRouter:router-compiler:0.6.2‘
}

Application类中

初始化
// 填写自己独特的scheme 避免与其他app重复
Router.init("frames");
package com.xiaozhuyisheng.jinjiedemos.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.thejoyrun.router.Router;
import com.thejoyrun.router.RouterActivity;
import com.xiaozhuyisheng.jinjiedemos.BaseActivity;
import com.xiaozhuyisheng.jinjiedemos.R;

import butterknife.OnClick;

/**
 * ActivityRouter 路由框架学习
 */

public class ActivityRouterDemo extends BaseActivity {


    @Override
    protected int initLayoutID() {
        return R.layout.activity_router;
    }

    @Override
    protected void initLogic() {

    }

    @OnClick({R.id.btnStart,R.id.btnParams})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnStart:
                Router.startActivity(this, "frames://first?uid=1000");
                break;
            case R.id.btnParams:
                // 支持uri 传参
                Intent intent = new Intent(this,RouterSecondActivity.class);
                intent.setData(Uri.parse("frames://second?uid=666"));
                intent.putExtra("name","zhangsan");
                startActivity(intent);
                break;
        }
    }
}
package com.xiaozhuyisheng.jinjiedemos.activity;

import android.widget.TextView;

import com.thejoyrun.router.Router;
import com.thejoyrun.router.RouterActivity;
import com.thejoyrun.router.RouterField;
import com.xiaozhuyisheng.jinjiedemos.BaseActivity;
import com.xiaozhuyisheng.jinjiedemos.R;

import butterknife.BindView;

@RouterActivity("first")
public class RouterFirstActivity extends BaseActivity {

    @BindView(R.id.content)
    TextView content;

    @RouterField("uid")
    private int uid;

    @Override
    protected int initLayoutID() {
        return R.layout.activity_router_first;
    }

    @Override
    protected void initLogic() {
        // 注入Bundle  Uri 的参数  支持类型:double float boolean  String  数据类型
        Router.inject(this);
        content.setText("传递过来的参数:" + uid);
    }
}
package com.xiaozhuyisheng.jinjiedemos.activity;

import android.widget.TextView;

import com.thejoyrun.router.Router;
import com.thejoyrun.router.RouterActivity;
import com.thejoyrun.router.RouterField;
import com.xiaozhuyisheng.jinjiedemos.BaseActivity;
import com.xiaozhuyisheng.jinjiedemos.R;

import butterknife.BindView;

@RouterActivity("second")
public class RouterSecondActivity extends BaseActivity {
    @BindView(R.id.content)
    TextView content;

    @RouterField("name")
    private String name;

    @RouterField("uid")
    private int uid;

    @Override
    protected int initLayoutID() {
        return R.layout.activity_router_first;
    }

    @Override
    protected void initLogic() {

        // 注入带过来的参数
        Router.inject(this);

        content.setText("传递过来的参数:\nuid=" + uid + "\nname=" + name);
    }
}

 

ActivityRouter 框架简单实用

标签:putextra   maven   sheng   .text   uid   bundle   epo   cti   zha   

原文地址:https://www.cnblogs.com/jiayonghua/p/10444077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!