How to handle screen orientation change when progress dialog and background thread active?

前端 未结 28 1622
轮回少年
轮回少年 2020-11-22 07:03

My program does some network activity in a background thread. Before starting, it pops up a progress dialog. The dialog is dismissed on the handler. This all works fine, exc

28条回答
  •  野性不改
    2020-11-22 07:50

    I faced this same problem, and I came up with a solution that didn't invole using the ProgressDialog and I get faster results.

    What I did was create a layout that has a ProgressBar in it.

    
    
    
    
    

    Then in the onCreate method do the following

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.progress);
    }
    

    Then do the long task in a thread, and when that's finished have a Runnable set the content view to the real layout you want to use for this activity.

    For example:

    mHandler.post(new Runnable(){
    
    public void run() {
            setContentView(R.layout.my_layout);
        } 
    });
    

    This is what I did, and I've found that it runs faster than showing the ProgressDialog and it's less intrusive and has a better look in my opinion.

    However, if you're wanting to use the ProgressDialog, then this answer isn't for you.

提交回复
热议问题