There is an online application. It has a lot of activations from which you need to send HTTP requests to the server. For this, a separate class is made - HttpLoader, through which it is convenient to make these same requests. Usage looks like this:

HttpLoader loader = new HttpLoader("http://site.com/script.php"); loader.setOnFinishListener(new HttpLoader.OnFinishListener() { public void onFinish(JSONObject json) { //Обработка данных } }); loader.start(); 

I would like to show AlertDialog, if you suddenly lost the connection to the network, with two buttons - “Retry” and “Exit application”. The problem is that in this class there is no link to the current Activity. How to implement it?

As I imagine, it is necessary that in each activation there is a code that is directly responsible for displaying this alert, and then some kind of interaction at the listener level with the HttpLoader class. Is this generally the right direction?

  • In HttpLoader, pass the context, and then make an AlertDialog out of it. - Helisia
  • @SuperCreeper, there is a risk to catch java.lang.IllegalArgumentException: View not attached to window manager. Especially if you use ProgressDialog. That is, the activation has already been closed, and the bootloader is still trying to work with the dialogue on this activation. - BArtWell
  • What prevents to call mAlert.dismiss(); in onDestroy() activate? - Helisia
  • So the alert was called from the loader, it does not know about it. Again, I would like to avoid writing the code for it in every activity, I want an elegant solution. - BArtWell

2 answers 2

Use Service . And there is a Context , and will allow you to easily organize interaction with the stream that performs network operations.

  • Well, because the service just like that AlertDialog can not be opened. You can, of course, dynamically create an Activity for this ... Do you mean it? - BArtWell
  • > Well, because of the service just because you can not open AlertDialog. Why is this? - falstaf

There are convenient types like http://loopj.com/android-async-http/

in the AsyncHttpResponseHandler AsyncHttpResponseHandler you can easily do your task

  • Well, I can implement this option in my class. The downside is that you have to drag the display code of the alert for all activations anyway, plus register this callback every time you access the network. Maybe you can somehow more elegantly solve the problem? - BArtWell