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.