Java 我如何从改型2拦截器启动登录活动
如何在改型2拦截器内启动登录活动?我正在使用dagger2注入依赖项,有什么最佳实践吗? 像这样说-我在LoginMvp.View mview上得到空指针Java 我如何从改型2拦截器启动登录活动,java,retrofit2,Java,Retrofit2,如何在改型2拦截器内启动登录活动?我正在使用dagger2注入依赖项,有什么最佳实践吗? 像这样说-我在LoginMvp.View mview上得到空指针 private final SharedPreferences preferences; @Inject LoginMvp.View mview; private String token; @Inject public AuthInterceptor(SharedPreferences preferences) { this.pr
private final SharedPreferences preferences;
@Inject LoginMvp.View mview;
private String token;
@Inject
public AuthInterceptor(SharedPreferences preferences) {
this.preferences = preferences;
}
public Response intercept(Chain chain) throws IOException {
Request.Builder builder = chain.request().newBuilder();
Request request = builder.build();
Response response = chain.proceed(request);
if (response.code() == 401) {
//start login activity
login();
}
}
//should call start loginActivity via intent and call finish
public void logout() {
mview.showLoginScreen();
}
您可以在应用程序上下文中创建应用程序上下文的静态对象,如下所示
public class App extends Application {
public static App context;
@Override
public void onCreate() {
super.onCreate();
context = this;
}
}
然后在AndroidManifest中将该类注册为
然后在mview.showLoginScreen()方法中对应用程序上下文进行null检查,如果不是null,则将登录活动作为
Intent intent = new Intent(App.context, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
App.context.startActivity(intent)