GithubHelp home page GithubHelp logo

luoyiqi / urlrouter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from reallin/urlrouter

0.0 1.0 0.0 778 KB

通过Url来进行activity的跳转,此方案应用很广。也可用于Hybrid混合编程中

Java 100.00%

urlrouter's Introduction

UrlRouter

通过Url来进行activity的跳转,此方案应用很广。也可用于Hybrid混合编程中。

功能描述

点击按钮时,通过url来判断并进行页面的跳转。首先界面如下:

点击后跳转到第二个Activity:

也可以带上参数:

实现思路

读取注解

利用来编译时读取注解,在编译时生成一个Map,它存储的就是Url与Activity的对应信息。下面就是生成的类。

public class SchemeDispatcher$$SchemeDatabase implements SchemeDataBase {
  @Override
  public Map<String, Class> getSchemeActivityClasses() {
    Map<String, Class> classes = new HashMap<String, Class>();
    classes.put("linxj://jumpActivity", com.example.linxj.urlrouter.JumpScheme.class);
    return classes;
  }
}

每一个Activity配置一个Scheme,用来跳转到这个Activity,这样就完全解藕了。

@UrlScheme("linxj://jumpActivity")
public class JumpScheme implements IScheme{


    @Override
    public void doWithScheme(Scheme scheme) throws Exception {
        Intent i = new Intent(scheme.getContext(),UrlJumpActivity.class);
        if(scheme.hasParameter("name")) {
            i.putExtra("name", scheme.getParameter("name"));
        }
        scheme.getContext().startActivity(i);
    }
}

跳转示例

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void jump(View view){
        new Scheme.Builder(MainActivity.this, Uri.parse("linxj://jumpActivity")).dispatch();
    }

    public void jumpParam(View view){
        new Scheme.Builder(MainActivity.this, Uri.parse("linxj://jumpActivity?name=lxj")).dispatch();
    }

}

上面代码jump是没有参数跳转,而jumpParam是带参数跳转。通过去生成的Map里找到对应启动类的Scheme来启动相应的类。

urlrouter's People

Contributors

reallin avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.