0

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.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Rupesh Bhandari
  • 76
  • 1
  • 12

1 Answers1

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.

Ratul Bin Tazul
  • 2,121
  • 1
  • 15
  • 23