Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!