Lance is absolutely right. I ran into this problem a few months ago.

Possible solution:
Hold a static handle to your currently active instance of your
activity (works only if you have at most one active instance of your
activity at any given time in your process).

public class MyActivity extends Activity {
    public static final MyActivity ACTIVE_INSTANCE;
    private final static int DIALOG_TASKING = 1;


    ProgressDialog mLoadingDialog;


    @Override
    public void onCreate(Bundle savedInstanceState) {

            ACTIVE_INSTANCE = this;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        new Task().execute();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

           ACTIVE_INSTANCE = null;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_TASKING:
                mLoadingDialog = new ProgressDialog(this);
                mLoadingDialog.setMessage("Loading stuff..");
                mLoadingDialog.setCancelable(true);
                return mLoadingDialog;
        }
        return super.onCreateDialog(id);
    }


    private static class Task extends AsyncTask<Void, Void, Void> {


        protected void onPreExecute() {
                ACTIVE_INSTANCE.showDialog(DIALOG_TASKING);
        }


                protected Void doInBackground(Void... unused) {
                        for (int i = 0; i < 4000000; i++) { }; // just
to take some time up
                        return null;
                }


                protected void onPostExecute(Void unused) {
                        ACTIVE_INSTANCE.dismissDialog
(DIALOG_TASKING);
                        Toast.makeText(ACTIVE_INSTANCE, "Finished..",
Toast.LENGTH_LONG).show();
                }
    }
}

You may need to put null checks in your code before using
ACTIVE_INSTANCE.
But, you get the idea :)


On Nov 7, 11:45 am, Lee Jarvis <ljjar...@googlemail.com> wrote:
> Ah ok, that makes sense. Thanks for your reply. I understand what
> you're saying, but in all honesty after trying another 3 examples I'm
> still unable to resolve this, could you possibly provide some kind of
> example for what would work?
>
> Seems if I use a reference to a ProgressDialog in my activity it'll
> leak, and if I use the showDialog() methods it'll continue forever.
>
> Thanks
>
> On Nov 7, 4:39 pm, Lance Nanek <lna...@gmail.com> wrote:
>
>
>
> > >private final class Task extends AsyncTask<Void, Void, Void> {
> > ...
> > >dismissDialog(DIALOG_TASKING);
>
> > A non-static inner class like this has a reference to the instance of
> > the class that created it. So that dismissDialog call probably goes to
> > the previous instance of your activity in this case. Not the current
> > one if there has been an orientation change and the activity has been
> > recreated.
>
> > >public void onDestroy() {
> > >dismissDialog(DIALOG_TASKING);
>
> > This is called too late to matter. Dialogs created by showDialog are
> > managed by the Activity class. It records which dialogs have to be
> > reshown when the activity is recreated due to an orientation change.
> > It does so right after the call to onSaveInstanceState. That happens
> > before onDestroy.
>
> > On Nov 7, 9:53 am, Lee Jarvis <ljjar...@googlemail.com> wrote:
>
> > > I apologise if I'm missing something or just being stupid.. But i've
> > > tried the following..
>
> > >     @Override
> > >     public void onDestroy() {
> > >         dismissDialog(DIALOG_TASKING);
> > >         super.onDestroy();
> > >     }
>
> > > The dialog is only dismissed if I DONT change orientation, otherwise
> > > the "finished" toast will show, but the ProgressDialog will never
> > > actually disappear.
>
> > > On Nov 7, 2:41 pm, Mark Murphy <mmur...@commonsware.com> wrote:
>
> > > > Lee Jarvis wrote:
> > > > > This code (kinda) works, the only problem is dismissing the dialog if
> > > > > the activity is recreated. I've tried dismissing it if mTaskComplete
> > > > > is true but I guess by that time it's lost reference to the
> > > > > ProgressDialog.
>
> > > > You may need to dismiss the dialog in onDestroy() and reopen it in
> > > > onCreate(), if the flag is true.
>
> > > > --
> > > > Mark Murphy (a Commons 
> > > > Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> > > > Android App Developer Books:http://commonsware.com/books- Hide quoted 
> > > > text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to