title: Retrofit: Redirect to LoginActivity if response code is 401
body: <p>How to start <code>LoginActivity</code> from the interceptor(non-activity class)? I have tried the code (<code>Interceptor</code>) below but not working for me. </p>
  
  <p>Interceptor</p>
  
  <pre><code>OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
              @Override
              public Response intercept(Chain chain) throws IOException {
  
                  Request newRequest = chain.request().newBuilder()
                          .addHeader("Authorization", "Bearer " + auth_token_string)
                          .build();
  
                  Response response =  chain.proceed(newRequest);
                  Log.d("MyApp", "Code : "+response.code());
                  if (response.code() == 401){
                      Intent intent = new Intent(SplashActivity.getContextOfApplication(), LoginActivity.class);
                      startActivity(intent);
                      finish();  //Not working
                      return response;
                  }
  
                  return chain.proceed(newRequest);
              }
          }).build();
  </code></pre>
  
  <p>This is the current solution I'm using, is there any better solution than this? This solution has to keep repeat on every api call.</p>
  
  <p>MainActivity</p>
  
  <pre><code>call.enqueue(new Callback&lt;Token&gt;() {
              @Override
              public void onResponse(Call&lt;Token&gt; call, Response&lt;Token&gt; response) {
                  if(response.isSuccessful())
                  {
                      //success
                  }
                  else
                  {
                      Intent intent = new Intent(MainActivity.this.getApplicationContext(), LoginActivity.class);
                      startActivity(intent);
                      finish();
                  }
              }
              @Override
              public void onFailure(Call&lt;Token&gt; call, Throwable t) {
  
              }
          });
  </code></pre>