Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
718 views
in Technique[技术] by (71.8m points)

progress bar - In android Why my ProgressBar Freezes?

i am showing Progress Bar at time of retrieving data from server and after retrieving data from server showing that data on chart but at time of plotting that data on chart my progress bar get freezes does any one have idea why this is so......

THANKS in advance...

private ProgressDialog pd;
private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {        
        Toast.makeText(context, "Please Wait...", Toast.LENGTH_LONG).show();
        Thread t = new Thread(new Runnable() {              
            @Override
            public void run() {
                functionDrawMyData();/*in this function i am accessing activity view and drawing data on that view at time of drawing my Progress bar Freezes */
            }
        });
        runOnUiThread(t);
        pd.dismiss();           
    }
};  

this handler i am using which is called after retrieving data finished and on button click i am getting data and showing progress bar

ImageButton myButton = (ImageButton) findViewById(R.id.myBtn);
pair1ChartButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {               
                pd = ProgressDialog.show(v.getContext(),"Please wait...","Retrieving data ...",true,
                        true,
                        new DialogInterface.OnCancelListener(){
                            @Override
                            public void onCancel(DialogInterface dialog) {

                            }
                        });
                Thread t = new Thread(new Runnable() {                      
                    @Override
                    public void run() {
                            getDataFromServer();//calling function to get data from server
                        handler.sendEmptyMessage(0);                    
                    }
                });
                t.start();
        }
    });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Remember android keeps the reference of dilogs in the memory. so that it does not need to recreate it again and again. so progress dialog works fine for the first time but hung/stuck for next time.

NOTE: android does not clear memory references of dilogs even after dismissing them.

there is method is Activity class named as removeDialog(int id) this will also clear memory references.

Here is how you can display and remove dialog

protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch(id){
          case 0:{
             dialog = ProgressDialog.show(this, "", 
                    "Loading. Please wait...", true);
             return dialog;
          }
             }

        return super.onCreateDialog(id);
    }

Now just call showDialog(0) to display dialog and removeDialog(0) to hide it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...