The introduction
All apps are composed of a large number of pages. Users can browse some pages as tourists, but most of the other pages require users to log in before browsing. In this case, check whether the current user logs in. If the user does not log in, log in first. If the user is already logged in, the user can jump to the cli directly. The similar implementation is as follows:
if(LoginUtils.getInstance().isLogin()){
startActivity(new Intent(context,DetailActivity.class));
}else{
startActivity(new Intent(context,LoginActivity.class));
}
Copy the code
However, if a large number of pages in an App are entered on the premise that the user has logged in, it is easy to cause a lot of code redundancy, which is not easy to maintain in the later period. We can deal with it through AOP implementation. Since the ARouter routing framework is used in the project, this problem can be handled through its interceptor.
Login interception is implemented by ARouter IInterceptor
The interceptor
@Interceptor(priority = 8, name = "login")
public class LoginInterceptorImpl implements IInterceptor {
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
String path = postcard.getPath();
LogUtils.d(path);
boolean isLogin = SPUtils.getInstance().getBoolean(Constant.SP_IS_LOGIN, false);
if(isLogin){
callback.onContinue(postcard);
}else{switch (path){// If you need to log in, go directly to this pagecase PageRouter.TEST_A:
case PageRouter.TEST_B:
callback.onInterrupt(null);
break; Default: callback.onContinue(postcard);break;
}
}
}
@Override
public void init(Context context) {
}
}
Copy the code
jump
public class LoginNavigationCallbackImpl implements NavigationCallback { @Override public void onFound(Postcard postcard) { } @Override public void onLost(Postcard postcard) { } @Override public void onArrival(Postcard postcard) { } @Override public void onInterrupt(Postcard postcard) { String path = postcard.getPath(); Bundle bundle = postcard.getExtras(); ARouter.getInstance().build(PageRouter.MINE_LOGIN) .with(bundle) .withString("path", path) .navigation(); }}Copy the code