0

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.

MMike
  • 598
  • 3
  • 23
  • Hi Mike, does the dialog need to be created every time it is shown? When getting a rotate you tend to get a oncreate/destroy. So if on the ondestroy you call dialog.dismiss(). It should remove all the old references – Chris Handy Jan 13 '15 at 16:09

2 Answers2

0

You should have a class level variable to your dialog.

Then you have several options for handling it. In onResume and/or onDestroy you can check to see if it is not null and dismiss it accordingly.

This depends on how you want the dialog to behave. If the dialog appears and then the phone rings, should it still be visible when the user returns? What if Android killed your app during the phone call and then reconstructed it when the user finished it?

Also, should the dialog "reappear" after rotation? In which case you should override onConfigurationChanged in order to handle that, too:

How to use onConfigurationChanged() and newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE in android 2.3.3

As far as "leaking" - if you don't mind the memory consumption as long as the activity is not GC'd, then having a reference to the activity is not a problem. You can set the object to null during the activity lifecycle if you are really concerned.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • So you recommend to instantiate the `Dialog` in my activity and pass the reference. And in `onPostExecute` i customize it and Show it. That way i have access to the valid reference in my `activity` where i can dismiss the dialog in `onPause` or `onResume`! Another question is it useful to create a holer class for the reference? Or is this not a recommended way? – MMike Jan 13 '15 at 18:16
0

I would probably do something like this, its a bit long winded.

Class A:

private Dialog m_dialog = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    //Create the dialog
}

@Override
protected void onDestroy() {
    m_dialog.dismiss();
    m_dialog = null;
}

public void showDialog(){
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if(m_dialog != null)
                m_dialog.show();
        }
    });
}

public void hideDialog(){
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if(m_dialog != null)
                m_dialog.hide();
        }
    });
}

ClassB:

protected void onPostExecute(Void result) {

    classAReference.showDialog();
}
Chris Handy
  • 366
  • 1
  • 5