如何显示ProgressDialog跨越推出一个新的活动?ProgressDialog

2023-09-06 10:31:00 作者:海岛情话

目标:有一个ProgressDialog,显示正在加载...,直到下一个活动是完全加载,并显示在屏幕上

Goal: Have a ProgressDialog which shows "Loading..." until next Activity is completely loaded and shown on screen.

试着用ProgressDialog背景和活动设置为原始活动。也试过用getApplicationContext()和getParentContext()。例外的最后两种方法。需要做到这一点作为目标的活动是缓慢的,因为非简单的布局文件来渲染。 (不能修复的,现在由于组织问题。)原来目标的活动需要1-2秒的OnCreate中,然后出现黑屏长达至少5秒钟,然后将其绘制。渲染仅仅是缓慢的。难道审查,层次浏览器,看到大量的红球,但现在无法修复。

Tried with ProgressDialog context and activity set to originating Activity. Also tried with getApplicationContext() and getParentContext(). Exceptions on the last two approaches. Need to do this as destination Activity is slow to render due to non-simple Layout file. (Cannot fix that right now due to organizational issues.) Turns out the destination Activity takes 1-2 seconds to OnCreate and then screen goes black for up to 5+ seconds then it paints. The rendering is just slow. Did review with Hierarchy Viewer and see lots of red balls but can't fix now.

阅读了一些相关的但还没有找到一个修复。例如。 Android - ?有什么不同方法之间的差异,以获得上下文

Read up on some related but haven't found a fix. E.g. Android - what's the difference between the various methods to get a Context?

例如。这两个崩溃。使用这个源活动也不行。

E.g. both of these crash. Using the "this" of source Activity doesn't work either.

// Context parentContext = this.getParent().getBaseContext();    
Context parentContext = this.getApplicationContext();
ProgressDialogMenuable theProgressDialog = new ProgressDialogMenuable(parentContext,this);
theProgressDialog.setTitle("yeeha");
theProgressDialog.setMessage("weewah");
theProgressDialog.setIndeterminate(true);
theProgressDialog.setCancelable(true);
theProgressDialog.show();

此外,奇怪的是,没事的时候我做的这种情况:                             theProgressDialog.show();                             ActivityHelper.changeActivity(此,五,InsMyHoldingsActivity.class,extraMap,-1,-1); 用户点击按钮,显示下一个活动,但ProgressDialog与活动启动冲突并没有什么实际情况以外的其他按钮变成黄色ontouch。下面的按钮的工作原理。除去ProgressDialog创作和它的作品。没有控制台消息记录。小offputting给开发商是肯定的。

Also, oddly, nothing happens when I do this: theProgressDialog.show(); ActivityHelper.changeActivity(this, v, InsMyHoldingsActivity.class, extraMap, -1, -1); User clicks button to show next activity but the ProgressDialog conflicts with the Activity launch and nothing actually happens other than the button becoming yellow ontouch. Button below works. removing ProgressDialog creation and it works. No console messages logged. A little offputting to the developer for sure.

推荐答案

您可以显示一个进度这样的对话框 -

You can show a progress dialog like this -

定义这个

private ProgressDialog pd = null;

在您的活动类

在您的onCreate将这个(不要直接的setContentView这里)

Put this in your onCreate (Dont setContentView directly here)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.pd = ProgressDialog.show(this, "Fancy App",
                    "Loading...Please wait...", true, false);
        // Start a new thread that will download all the data
        new IAmABackgroundTask().execute();

    }

//背景繁重

// Background heavy lifting

class IAmABackgroundTask extends
        AsyncTask<String, Integer, Boolean> {
    @Override
    protected void onPreExecute() {
        // showDialog(AUTHORIZING_DIALOG);
    }

    @Override
    protected void onPostExecute(Boolean result) {

        // Pass the result data back to the main activity
        ActivityName.this.data = result;

        if (ActivityName.this.pd != null) {
            ActivityName.this.pd.dismiss();
        }

        setContentView(R.layout.main);


    }

    @Override
    protected Boolean doInBackground(String... params) {

        //Do all your slow tasks here but dont set anything on UI
                    //ALL ui activities on the main thread 

        return true;

    }

}

另外经过情况:http://developer.android.com/training/improving-layouts/index.html用于优化布局的表现。 同时使用Traceview寻找瓶颈

Also go through this :http://developer.android.com/training/improving-layouts/index.html for optimizing layout performance. Also Use Traceview to look for bottlenecks