Hello
Hello

Reputation: 119

Android: Thread with progressBar and setcurrentTab

   final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, "", 
                        "Loading. Please wait...", true);
                Thread ProgressThread = new Thread() {
                    @Override
                    public void run() {
                        try {
                                     sleep(3000);   
                                     Pdialog.dismiss();

                        } catch(InterruptedException e) {
                            // do nothing
                        } finally {

                        }
                    }
                };
                ProgressThread.start();
                TabHost1 TabHost1Object2 = new TabHost1();
                TabHost1Object2.tabHost.setCurrentTab(2);

The problem I have with this thread is that it sets the current tab before the progress dialog starts. What have i done wrong ? I want the dialog to run and dismiss, and after thread is done set tab.

Upvotes: 0

Views: 352

Answers (2)

Andro Selva
Andro Selva

Reputation: 54322

The thing is that,you are starting a thread which will not affect your main UI. So what eventually happens is that, your thread will run separately which will now allow the next lines of your code to be executed. So in your case,

            TabHost1 TabHost1Object2 = new TabHost1();
            TabHost1Object2.tabHost.setCurrentTab(2);

these lines will be executed irrespective to your thread which is also getting executed simultaneously. So what you can do here is you can either go for AsyncTask or create handlers to handle this part of your code. You have to change your code like this.

Do this in your onCreate()

           Handler handler;
    handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
         Pdialog.dismiss();     
             TabHost1 TabHost1Object2 = new TabHost1();
            TabHost1Object2.tabHost.setCurrentTab(2);

         }
       };

And now in your thread,call the handler like this,

  final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, "", 
                    "Loading. Please wait...", true);
            Thread ProgressThread = new Thread() {
                @Override
                public void run() {
                    try {
                                 sleep(3000);   


                    } catch(InterruptedException e) {
                        // do nothing
                    } finally {

                             handler.sendEmptyMessage(0);
                    }
                }
            };
this will allow your tabhost to wait until the thread gets executed and will come into view after thread finishes execution. 

Upvotes: 0

Adil Soomro
Adil Soomro

Reputation: 37729

use AsyncTask for this

some hints:

public class BackgroundAsyncTask extends  AsyncTask<Void, Integer, Void> {  
    int myProgress;
    @Override
    protected void onPostExecute(Void result) {
    TabHost1 tab = new TabHost1();
    tab.tabHost.setCurrentTab(2);
    progressBar.dismiss();

    }
    @Override
    protected Void doInBackground(Void... params) {   
        while(myProgress<100){
            myProgress++;
            publishProgress(myProgress);
            SystemClock.sleep(100);
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer p) {   
        progressBar.setProgress(p);
    }

}

Upvotes: 2

Related Questions