Android routing framework: simple use of ARouter interceptor


foreword

This is my second blog, which is about the simple use of ARouter interceptors. Friends who have never learned ARouter can read my previous blog Android Routing Framework: Simple Use of ARouter


Tip: Refer to the original text to explore the Android routing framework - the basic use of ARouter (1)

1. Interceptor

The biggest highlight of ARouter's routing framework is its interceptor. So what can we use interceptors for? For example, we can use it to judge whether the token has expired when clicking the Jump to Home button on the initial page. If it expires, we will jump to the login page to log in again.

2. Use steps

1. The creation of the interceptor

insert image description here
First we define a class, then we must add aInterceptorNote, the smaller the input priority, the higher the priority, and the int value of this priority must be unique, there cannot be two equal priorities, otherwise the program will crash. When executing routing, the interceptors will be executed sequentially according to the priority.
Then we implement the IInterceptor interface and rewrite the two methods init() and process(). The init() method will be executed when ARouter is initialized, and the process() method will be executed when the interceptor is executed.

2. Use of interceptors

Determine our task: First, our initial page is on the MainActivity page, click the button to set the jump route to SecondActivity, then intercept this route through the interceptor, and let us jump to LoginActivity.

Writing an interceptor

Interceptor code

if (postcard?.path.equals(Constance.ACTIVITY_SECOND)) {
    
    
            Log.e("UseIInterceptor", "进行了拦截处理:" + postcard?.path.toString())
            //拦截路由
            callback.onInterrupt(null)
            ARouter.getInstance().build(Constance.ACTIVITY_LOGIN).navigation()
        }else{
    
    
            //交还控制权
            callback.onContinue(postcard)
        }

First of all, we use the postcard to obtain the routing path we jump to, and judge if the path is to jump toSecondActivityYes, let the interceptor intercept directly, and then jump to LoginActivity.
callback.onInterrupt(null) This code allows the route to be intercepted, and then executes the new route and jumps directly to LoginActivity.
If the path does not jump to SecondActivity, we execute the callback.onContinue(postcard) code to return control. (! ! Be sure to return the control, otherwise even if the interception conditions are not met, he will not perform subsequent routing jumps
MainActivity code

    ARouter.getInstance().build(Constance.ACTIVITY_SECOND)
//                .withParcelable("san", person)
                .navigation(this, object : NavigationCallback {
    
    
                    override fun onFound(postcard: Postcard?) {
    
    
                        //路由目标被发现时调用(被拦截器拦截前会调用)
                        Log.e(TAG, "onFound:")
                    }

                    override fun onLost(postcard: Postcard?) {
    
    
                        //路由丢失之后调用(地址错误找不到对应的路径)
                        Log.e(TAG, "onLost:")
                    }

                    override fun onArrival(postcard: Postcard?) {
    
    
                        //路由到达之后调用(onFound执行后如果未被拦截就执行)
                        Log.e(TAG, "onArrival:")
                    }

                    override fun onInterrupt(postcard: Postcard?) {
    
    
                        //路由被拦截时调用
                        Log.e(TAG, "onInterrupt:")
                    }
               })

If we do not have an interceptor to intercept and the path is correct, the normal steps should be the onFound() method first and then the onArrival() method. If it is intercepted, the onFound() and onInterrupt() methods will be executed.
If the routing path is wrong, the onLost method will be called directly.

Guess you like

Origin blog.csdn.net/XL1583135614/article/details/114285421