Last active
October 2, 2017 18:18
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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