I am trying to display a dialog from Retrofit interceptor when few parameters are not satisfied.
But I got android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
exception when trying to show dialog.
Here is my code.
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(ShieldSquare.applicationContext)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Are you sure to Exit")
.setMessage("Exiting will call finish() method")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
//set negative button
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
Toast.makeText(ShieldSquare.applicationContext,
"Nothing Happened", Toast.LENGTH_LONG).show();
}
});
Runnable runnable = new Runnable() {
@Override
public void run() {
alertDialog.create().show();
}
};
new Handler(Looper.getMainLooper()).post(runnable);
Above code running on Interceptor before Retrofit's chain is proceed.
ssResponse = chain.proceed(originalRequest);