So I have an Asynctask class to perform the next action 5 seconds after clicking a button.
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delay = new delayMessageAsyncTask();
delay.setProgressBar(delaySendProgressWheel);
delaySendProgressWheel.setProgress(0);
delaySendProgressWheel.setSpinSpeed(0.21f);
mButton.setVisibility(ImageView.GONE);
delaySendProgressWheel.setVisibility(ProgressWheel.VISIBLE);
delay.execute();
//performAction();
}
});
class delayMessageAsyncTask extends AsyncTask<Void, Integer, Void> {
int myProgress;
@Override
protected void onPreExecute() {
myProgress = 0;
super.onPreExecute();
}
@Override
protected void onPostExecute(Void result) {
edit_text.setText("");
mButton.setVisibility(ImageView.VISIBLE);
delaySendProgressWheel.setVisibility(ProgressWheel.GONE);
msgAdapter.setArrayList(message);
msgAdapter.notifyDataSetChanged();
listView.smoothScrollToPosition(msgAdapter.getCount()-1);
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... params) {
while(myProgress<100){
myProgress+=20;
publishProgress(myProgress);
try {
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
performAction();
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
delaySendProgressWheel.setProgress((float)values[0]);
}
The thing is, the performAction()
is quite complicated since it contains both background thread and UI thread, so I have to put a portion inside doInBackground
and update the UI onPostExecute
. But as I said only after exactly 5 seconds can the action be performed so it stops for a while before reaching onPostExecute
. I have tried moving the performAction()
to onPostExecute
, but it cause the UI to freeze. Ideally, I want to know if theres any way to stop executing the code after the execute()
method in onClick()
so I can put the performAction()
there.