3

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);

A.Bidusha
  • 43
  • 6
  • alert dialog not show into retrofit Interceptor. it show only activity or fragment. when you call api that time show alert dialog. –  Jul 03 '18 at 11:19
  • @AndroidTeam Thanks. I am developing an SDK for android which will detect misuse of application and stops api calls followed by showing a dialog. How such thing can be implemented in context of Retrofit interceptor? – A.Bidusha Jul 03 '18 at 11:23

1 Answers1

5

Generally, we only display dialogs from activities, so the context ShieldSquare.applicationContext is unable to show the AlertDialog.

There are two methods which can meet your needs:

First one, use a special permission android.permission.SYSTEM_ALERT_WINDOW. Before your alertDialog.show();, add:

alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

And add the permission below to the AndroidManifest.xml.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Then you can use ShieldSquare.applicationContext for dialog builder.

Second one, you can update your ShieldSquare.applicationContext to the most recent activity, then the ShieldSquare.applicationContext will always be an activity's context:

public abstract class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShieldSquare.applicationContext = this;
    }
}

And I found a few approaches about using BroadcastReceiver to show the dialogs, you can also have a look, see the SO answer and this blog post.

Hong Duan
  • 4,234
  • 2
  • 27
  • 50