Skip to content

Instantly share code, notes, and snippets.

@mumayank
Last active October 2, 2017 18:18
Show Gist options
  • Save mumayank/cca2870abe109a243da324189715a7c5 to your computer and use it in GitHub Desktop.
Save mumayank/cca2870abe109a243da324189715a7c5 to your computer and use it in GitHub Desktop.
Easily do background tasks, update UI during start, inbetween and in the end.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new BgAsyncTask().execute(null, null, null);
}
class BgAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// do something
}
@Override
protected Void doInBackground(Void... params) {
publishProgress(null);
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
// do something
}
@Override
protected void onPostExecute(Void aVoid) {
// do something
}
}
}
@Override
protected void onDestroy() {
if (bgAsyncTask != null) {
bgAsyncTask.cancel(true);
}
super.onDestroy();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment