简体   繁体   English

在加载其他活动时显示ProgressDialog

[英]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. 当用户在活动1中按下按钮时,活动2将打开。 Activity 2 has to load some data that has been stored in the SharedPreferences. 活动2必须加载一些已存储在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). 活动2必须在onCreate()中做很多事情,因此这就是为什么花一些时间才能打开它的原因(尤其是在较慢的设备上)。 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. 与我见过的其他情况不同,Activity不必从Internet下载数据,因此我不可以选择使用AsyncTask,因为打开Activity花费很长时间的原因不是Internet,但原因是必须加载5个列表视图,并且必须进行大量计算才能正确处理数据。

So how do you display a ProgressDialog in this case, while Activity 2 is loading? 那么在活动2加载时如何在这种情况下显示ProgressDialog?

So how do you display a ProgressDialog in this case, while Activity 2 is loading? 那么在活动2加载时如何在这种情况下显示ProgressDialog?

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. 不必担心,因为即使您显示ProgressDialog它也会冻结,因为您的Activity会很努力地阻塞主UI线程。 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: 如果您知道布局创建/数据构建(或您在onCreate()方法中所做的任何事情)将花费一些时间,那么用户会注意到:

  • make your initial layout to contain a ProgressBar to indicate the work being done to the user 使您的初始布局包含一个ProgressBar以向用户指示正在完成的工作
  • start a thread/AsyncTask to do the heavy lifting and exit onCreate() 启动线程/ AsyncTask进行繁重的工作并退出onCreate()
  • when the thread is done with its job build the layout replacing the initial ProgressBar 当线程完成其工作时,构建布局以替换初始的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). 也许您还可以改进一些东西(五个ListViews似乎有点奇怪)。

public class TestActivity extends Activity { 公共类TestActivity扩展了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);
    };
};

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM