0

I have to load data in the same activity, from different request

request 1 will load some data and request 2 will load data from another url

now I am calling them sequential

request1.run () request2.run ()

the problem now that i need to show dialog before them and hide them after all data finished

can anyone tell me what is the best way to do that ?

Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175

2 Answers2

1

AsyncTask has working:

doInBackground: Code performing long running operation goes in this method. When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed.

onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.

onPreExecute: This method is called before doInBackground method is called.

class Login extends AsyncTask {

    private final static String TAG = "LoginActivity.Login";

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        Log.d(TAG, "Executando onPreExecute Login");
        //inicia diálogo de progresso, mostranto processamento com servidor.
        progressDialog = ProgressDialog.show(LoginActivity.this, "Autenticando", "Contactando o servidor, por favor, aguarde alguns instantes.", true, false);
    }


    @SuppressWarnings("unchecked")
    @Override
    protected String doInBackground(Object... parametros) {
        Log.d(TAG, "Executando doInBackground Login");
        Object[] params = parametros;
        HttpClient httpClient = (HttpClient) params[0];
        List<NameValuePair> listaParametros = (List<NameValuePair>) params[1];
        String result = null;
        try{
        result = HttpProxy.httpPost(AnototudoMetadata.URL_AUTENTICACAO, httpClient, listaParametros);
        }catch (IOException e) {
            Log.e(TAG, "IOException, Sem conectividade com o servidor do Anototudo! " + e.getMessage());
            e.printStackTrace();
            return result;
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);

        if (result == null || result.equals("")) {
            progressDialog.dismiss();
            Alerta
                    .popupAlertaComBotaoOK(
                            "Dados incorretos",
                            "Os dados informados não foram encontrados no Sistema! Informe novamente ou cadastre-se antes pela internet.",
                            LoginActivity.this);
            return;
        }

        Log.d(TAG, "Login passou persistindo info de login local no device");
        ContentValues contentValues = new ContentValues();
        contentValues.put(AnototudoMetadata.LOGIN_EMAIL, sLogin);
        contentValues.put(AnototudoMetadata.LOGIN_SENHA, sSenha);
        contentValues.put(AnototudoMetadata.LOGIN_SENHA_GERADA, result);
        LoginDB loginDB = new LoginDB();
        loginDB.addLogin(LoginActivity.this, contentValues);
        Log.d(TAG, "Persistiu info de login no device, redirecionando para menu principal do Anototudo");
        Log.d(TAG, "O retorno da chamada foi ==>> " + result);
        // tudo ok chama menu principal
        Log.d(TAG, "Device foi corretametne autenticado, chamando tela do menu principal do Anototudo.");

        String actionName = "br.com.anototudo.intent.action.MainMenuView";
        Intent intent = new Intent(actionName);
        LoginActivity.this.startActivity(intent);
        progressDialog.dismiss();
    }

} 
0

Try using an async task, along the lines of

private class RunRequestsTasks extends AsyncTask<Void,Void,Void>{
    private Context tContext;

    @Override
    protected void onPreExecute(){
        //Build dialog here
    }
    @Override
    protected void doInBackground(Void... voids) {
        request1.run();
        request2.run()
        return null;
    }

    @Override
    protected void onPostExecute(Void voidRet){
        //Dismiss dialog here
    }
}

It's difficult to give a more tailored answer without more information though.