I am saving data on onPause
method to SQLite
database.
myObject.saveEntry(data)
. AsyncTask.execute(new Runnable(){})
savedSuccess(message)
is calledActivity
method showMessage(message)
is calledMy doubt is on how to handle the orientation changes. When the orientation changes,
onPause
is called and saveEntry
method is called.onDestroy
is calledsavedSuccess(message)
method callback gets executed now (which was called from the previous instance of the Activity)showMessage(message)
is called and toast is shownMy questions are
AsyncTask
from executing in the onDestroy
methodWhat you are seeing is memory leaking - the AsyncTask retains a reference to the Activity it was declared in and is preventing the Android runtime from garbage collecting it. To answer your questions:
AsyncTask cannot be stopped with a command. You can request to cancel it in onDestroy() but it will still do it only after the doInBackground() method has executed which in your case will be the Runnable.
You have the option of using a static nested AsyncTask which will not hold a reference to the Activity it resides in - the caveat is that you will not be able to reference anything from the Activity but that shouldn't be a big problem for your case.