Arouter的使用

依赖
implementation 'com.alibaba:arouter-api:1.4.1'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
        }

然后在Application里面进行初始化。

if (BuildConfig.DEBUG){
            ARouter.openLog();
            ARouter.openDebug();
        }
        
ARouter.init(this);
不带参跳转
ARouter.getInstance()
        .build("/main/LogoutActivity")
        .navigation();
        
        
@Route(path = "/main/LogoutActivity")
public class LogoutActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_out);
    }
}
带参跳转
ARouter.getInstance()
        .build("/main/login")
        .withString("path","您好")
        .navigation();
        
        
@Route(path = "/main/login")
public class LoginActivity extends AppCompatActivity {
    
    /*@Autowired
    public String path;*/

    @Autowired(name = "path")
    public String pasts;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.e("zzf","跳转过来了");
        ARouter.getInstance().inject(this);
        TextView textView = findViewById(R.id.tv);
        textView.setText(pasts);
    }
}        

在这需要注意下面几点:

1、被@Autowired修饰的成员变量不能背pravite修饰,不然运行时会报错

2、如果@Autowired没有指定name,则成员变量的名字必须为传过来的key,不然没有效果

@Autowired
public String path

3、如果@Autowired指定name,则成员变量的名字可以随便写。

@Autowired(name = "path")
public String pasts;
uri跳转
Uri uri = Uri.parse("test://com/main/UrlLoginActivity");
ARouter.getInstance().build(uri).navigation()
@Route(path = "/main/UrlLoginActivity")
public class UrlLoginActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_url);
    }
}

            
                
                
                
                
            
        
startActivityForResult
ARouter.getInstance()
        .build("/main/ForResultActivity")
        .withInt("paths",1)
        .navigation(MainActivity.this,0);
        
@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0){
            if (resultCode == 1){
                btn_ForResult.setText(data.getIntExtra("name",0)+"");
            }
        }
    }
@Route(path = "/main/ForResultActivity")
public class ForResultActivity extends AppCompatActivity {

    @Autowired
    public int paths;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_for_result);
        TextView textView = findViewById(R.id.tv);
        textView.setText(paths+"");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(1,new Intent().putExtra("name",666));
                finish();
            }
        });
    }
}
 很多博客都说requestCode必须>0
 其实看源码0是可以的
 if (requestCode >= 0) {  // Need start for result
    if (currentContext instanceof Activity) {
            ActivityCompat.startActivityForResult((Activity) currentContext, intent, requestCode, postcard.getOptionsBundle());
    } else {
            logger.warning(Consts.TAG, "Must use [navigation(activity, ...)] to support [startActivityForResult]");
     }
  } else {
        ActivityCompat.startActivity(currentContext, intent, postcard.getOptionsBundle());
  }
                
跳转拦截
@Interceptor(priority = 1)
public class TestInterceptor implements IInterceptor {
    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        if (postcard.getPath().equals("/main/login")){
            ARouter.getInstance().build("/app/Mainactivity").navigation();
        }else {
            callback.onContinue(postcard);
        }
    }

    @Override
    public void init(Context context) {

    }
}
降级策略
Uri uri = Uri.parse("test://com/main/UrlLoginActivity");
                ARouter.getInstance().build(uri).navigation(MainActivity.this, new NavigationCallback() {
                    @Override
                    public void onFound(Postcard postcard) {

                    }

                    @Override
                    public void onLost(Postcard postcard) {
                        /**
                         * 在这做降级处理,传递的path有问题时,我们就可以跳转到其他地方
                         */
                    }

                    @Override
                    public void onArrival(Postcard postcard) {
                        /**
                         * 跳转成功的回调
                         */
                    }

                    @Override
                    public void onInterrupt(Postcard postcard) {

                    }
                });
            }

具体代码看 https://github.com/fengyuehan/Test/tree/master/app

你可能感兴趣的:(Arouter的使用)