Lets assume i have an activity
public class ClassA extends Activity {
protected void onCreate(Bundle savedInstanceState)
{
....
}
in onCreate
i create an object of a class which extends AsyncTask
:
MyAsynctask classB = new MyAsyncTask(ClassA.this); //pass activity-context for dialog
classB.execute();
in onPostExecute
of MyAsynctask
i want to show a dialog.
protected void onPostExecute(Void result) {
Dialog myCustomDialog = new Dialog(activityContext);
...
myCustomDialog.show();
}
Now i have following Problem:
After for example i rotate my device a new object of MyAsynctask
is created and executed. That's ok and the way i want it! But if the dialog from the previous reference of MyAsynctask
isn't closed by the user till he rotates the device, a new dialog is shown above the old one and more worse i am leaking memory.
Question:
1) What is a good way to hold a valid reference to the dialog so that i can call myCustomDialog.dismiss()
before i create a new instance of MyAsynctask
and therefore a new dialog
?
2) Will myCustomDialog.dismiss()
clear the strong reference so that GC is able to do his work and i'm not leaking memory anymore? Or do i need to set the strong reference to something like this after dismissing the dialog: myCustomDialog = null
?
I'm relatively new to android and i want to learn a good pattern of how to accomplish this scenario :)
I appreciate any help.