Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
getting into else statement because of AsyncTask ? possibly?

I have two classes, MainActivity and Tempo and their codes

MainActity.java

 public class MainActivity extends AppCompatActivity {
        Tempo m_context;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        m_context = Tempo.getInstance();
    }
   }

public void button_clicked(View v){
         if( m_context.connect("a1",1)){
        setTitle("Yeah!");
          }else {
        setTitle("No");
        }
    }

and Tempo.java

public class Tempo{

    public boolean isConnected=false;
    private static Tempo insta;
    private Tempo() { }

    public synchronized static Tempo getInstance() {
            if (insta == null) {
                insta = new Tempo();
            }
            return insta;
    }

    public boolean connect(String a, int aa) {
    new DoTask().execute(a);
        return isConnected;
    }

    public class DoTask extends AsyncTask<String,Void,Boolean> {

        @Override
        protected Boolean doInBackground(String... params) {
            boolean result = true;
            try {
                //If I'm here everything is okay
            } catch (IOException e) {
                //If I'm here everything is !okay
                result = false;
            }
            return result;
        }

        @Override
        protected void onPostExecute(Boolean result)
        {

            isConnected = result;

        }
    }
}

Even though my work is done inside Tempo perfectly which means the boolean vars in Tempo class "result" and "isConnected" is meant to be true. No doubt they transformed to True after my work but the main issue is using those vars, I'm not able to go in if block of MainActivity.. which will change my title to "Yeah!". As per me it's because of AsyncTask throwing task in background hence making my vars remains the same(false) for some particular time??

Well, I need AsyncTask so that the UI won't stuck.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

onPostExecute runs in the UI thread. so you can update the ui in this method instead of your MainActivity.

public class Tempo{
Activity activity;

public boolean isConnected=false;
private static Tempo insta;
private Tempo() { }

public synchronized static Tempo getInstance() {
        if (insta == null) {
            insta = new Tempo();
        }
        return insta;
}

public boolean connect(String a, int aa,Activity a) {
this.activity= a;
new DoTask().execute(a);
    return isConnected;
}

public class DoTask extends AsyncTask<String,Void,Boolean> {

    @Override
    protected Boolean doInBackground(String... params) {
        boolean result = true;
        try {
            //If I'm here everything is okay
        } catch (IOException e) {
            //If I'm here everything is !okay
            result = false;
        }
        return result;
    }

    @Override
    protected void onPostExecute(Boolean result)
    {

        if( result){
          Activity.setTitle("Yeah!");
        }else {
          Activity.setTitle("No");
        }

    }
}
}

So as you can see I am passing the activity as a parameter then using that to update the UI in onPostExecute() method.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda