Hi all,

I have a main activity that launches a sub activity when the user
clicks a button as follows:

private OnClickListener buttonDirectoryListener = new OnClickListener
() {
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(Main.this, Sub.class);
        startActivity(intent);
    }
}

Unfortunatly, the onCreate method of the sub activity can take a long
time (server request, xml parsing, custom layout creation, ...). So I
want to display an indeterminate ProgressDialog while the sub activity
is being created and then dismiss it when main activity is no longer
visible. I found a few threads claiming that a separate thread
is needed to run the progress dialog. So I updated my code as follows:

ProgressDialog pd;

private OnClickListener buttonDirectoryListener = new OnClickListener
() {
    public void onClick(View v) {
        pd = new ProgressDialog(this);
        pd.setIndeterminate(true);
        pd.show();
        new Thread() {
            public void run() {
                Intent intent = new Intent();
                intent.setClass(Main.this, Sub.class);
                startActivity(intent);
            }
        }.start();
    }
}

@Override
public void onStop() {
    super.onStop();
    pd.dismiss();
}

It seems to work sometime. But most of the time, the ProgressDialog is
not showing (or disappareing fastly), I only get a black sreen until
the sub activity is shown. Poor effect. I guess it might lead to an
ANR dialog in real situation. I guess the main activity receives
onStop event before the sub activity is shown. I would like to
indicate loading through the ProgressDialog until the sub activity
gets ready to be displayed.

I'm convinced other developers are facing such a problem and I'm
probably doing something wrong. Any help is welcome. Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to