Xander
Xander

Reputation: 5587

Show ProgressDialog while other activity is loading

This question has been asked before, but the answers I've found weren't useful in my case.

When the user presses a button in Activity 1, Activity 2 is opened. Activity 2 has to load some data that has been stored in the SharedPreferences. Activity 2 has to do quite a lot in the onCreate(), so that's why it takes a little while to open it (especially on slower devices). Unlike the other cases I've seen, the Activity doesn't have to download data from the internet, so using an AsyncTask isn't an option for me, because the reason why it takes long to open the Activity isn't internet, but the reason is that is has to load 5 listviews and it has to do a lot of calculations to process the data properly.

So how do you display a ProgressDialog in this case, while Activity 2 is loading?

Upvotes: 2

Views: 1758

Answers (2)

Zulfiqar Chandio
Zulfiqar Chandio

Reputation: 263

public class TestActivity extends Activity {

ProgressDialog progressDialog;

private Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    context = this;

    showProgress("Data loading...");

    new Thread() {
        public void run() {

            try {

                boolean sucess = loadData();
                if (sucess) {
                    Message alertMessage = new Message();
                    alertMessage.what = 1;
                    handle.sendMessage(alertMessage);
                } else {
                    Message alertMessage = new Message();
                    alertMessage.what = 2;
                    handle.sendMessage(alertMessage);
                }

            } catch (Exception e) {
                // TODO: handle exception
            }

        }
    }.start();

}

private boolean loadData() {

    // load data here
    return true;
}

private void showProgress(String msg) {
    progressDialog = ProgressDialog
            .show(TestActivity.this, null, msg, true);
}

private void hideProgress() {
    if (progressDialog != null)
        progressDialog.dismiss();
}

Handler handle = new Handler() {

    public void handleMessage(android.os.Message msg) {
        hideProgress();
        if (msg.what == 1) {
            Toast.makeText(context, "Data Loaded Sucessfully",
                    Toast.LENGTH_SHORT).show();

        } else if (msg.what == 2) {
            Toast.makeText(context, "Data Loading failed ",
                    Toast.LENGTH_SHORT).show();
        }

        super.handleMessage(msg);
    };
};

}

Upvotes: 0

user
user

Reputation: 87064

So how do you display a ProgressDialog in this case, while Activity 2 is loading?

This shouldn't be your concern because even if you show a ProgressDialog it will simply freeze as you'll block the main UI thread as your Activity will struggle to do its stuff. If you know the layout creation/data building(or whatever you do in the onCreate() method) will take some time that will be noticed by the user then:

  • make your initial layout to contain a ProgressBar to indicate the work being done to the user
  • start a thread/AsyncTask to do the heavy lifting and exit onCreate()
  • when the thread is done with its job build the layout replacing the initial ProgressBar

This way your activity will start with the working/loading indicator and will make the layout when it's available. Maybe you could also improve stuff(five ListViews seems a bit strange).

Upvotes: 4

Related Questions