开发者

showing dialog while loading layout by setContentView in background

I am using below code where , i want to show dialog in front and loading content in background but not able to do the same . Please advise.

dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
runOnUiThread(new Runnable(){
  public void run() {
   开发者_开发百科 setContentView(R.layout.main_layout);
    dialog.dismiss();
  }
});


I got solution by reading below link and implemented code as below: http://developer.android.com/guide/appendix/faq/commontasks.html#threading

int glb=0,glbtotal=3;
final Handler mHandler = new Handler();

// Create runnable for posting
final Runnable mUpdateResults = new Runnable() {
    public void run() {
        updateResultsInUi();
    }
};

public void open_next_screen()
    {
            dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
            startLongRunningOperation();
     }
private void updateResultsInUi() {

    // Back in the UI thread -- update our UI elements based on the data in mResults
    switch(glb)
    {
    case 0:
         setContentView(R.layout.main_layout);
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

        break;
    case 1:
    part2();

    break;
    case 2:
     part3();

    break;
    }

}

protected void startLongRunningOperation() {

    // Fire off a thread to do some work that we shouldn't do directly in the UI thread
    Thread t = new Thread() {
        public void run() {


            for(glb=0;glb<glbtotal;glb++)
            {
                mHandler.post(mUpdateResults);

            switch(glb)
            {
            case 0:
                part1();
                break;

            }
            }
              dialog.dismiss();
        }
    };
    t.start();
}


There's an example of how to do loading in a separate thread on the Android developer's website. To see the actual code, expand the view at the end of the section on Progress Dialogs.

However, for your particular situation, you need to rework how you're doing things. What is taking so long that you need a progress dialog for it? You should load that in the 2nd thread, while displaying some kind of temporary layout with setContentView. Then once the thread finishes loading, either call setContentView again, or change the text, images or whatever that you loaded with the thread.

Lastly: If you're really new to programming in Android, I'd avoid trying to use separate threads and complicated loading things for now. It's a bit tricky to do and there's a lot to understand first.


Sounds like you want a splash screen or something but setting the content view should not be done in a thread. Post the layout.xml, what is in there that is taking it so long to render.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜