Skip to content Skip to sidebar Skip to footer

Run Progress Bar While Switching Activity

im stuck in a situation where i am switching from activity 1 to activity 2. i am using Thread.sleep(5000) to start another activity after 5 seconds But the progress bar which i wan

Solution 1:

Change your OnClickListener for this. This will not block your main thread, as you are doing (that explains why your application freezes for 5 seconds):

next.setOnClickListener(newView.OnClickListener() {
    publicvoidonClick(View view) {
        newAsyncTask<Integer, Long, Boolean>()
        {
            ProgressDialog pd;

            @OverrideprotectedBooleandoInBackground(Integer... params)
            {
                pd = newProgressDialog(Activity1.this);
                pd.setTitle("Loading Activity");
                pd.setMessage("Please Wait ...");
                pd.setMax(params[0]);
                pd.setIndeterminate(false);
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

                publishProgress(0L);

                long start = System.currentTimeMillis();
                long waitTime = params[0] * 1000;
                try
                {
                    while (System.currentTimeMillis() - start < waitTime)
                    {
                        Thread.sleep(500);
                        publishProgress(System.currentTimeMillis() - start);
                    }
                }
                catch (Exception e)
                {
                    returnfalse;
                }

                returntrue;
            }

            @OverrideprotectedvoidonProgressUpdate(Long... values)
            {
                if (values[0] == 0)
                {
                    pd.show();
                }
                else
                {
                    pd.setProgress((int) (values[0] / 1000));
                }
            }

            @OverrideprotectedvoidonPostExecute(Boolean result)
            {
                pd.dismiss();
                Intent myIntent = newIntent(view.getContext(), activity2.class);
                startActivityForResult(myIntent, 0);
            }
        }.execute(5);
    });

Solution 2:

Don't use Thread.sleep() - it is the root to all evil. Instead, use a Handler and its postDelayed( Runnable, time )-method like below:

publicclassActivity1extendsActivity  {          
  publicvoidonCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Buttonnext= (Button) findViewById(R.id.B);
  final ProgressBar  p=(ProgressBar) findViewById(R.id.pr);
  next.setOnClickListener(newView.OnClickListener() {
    publicvoidonClick(View view) {
      p.setVisibility(4);

      finalHandlerhandler=newHandler();
      handler.postDelayed(newRunnable() {
        @Overridepublicvoidrun() {
          IntentmyIntent=newIntent(view.getContext(), activity2.class);
          startActivityForResult(myIntent, 0);
        }
      }, 5000);
    }
  });   
}

Solution 3:

You better use an AsyncTask for this purpose.Using threads like that in your activity is not proper and can lead to some fails.Check this docs about AsyncTask.

http://developer.android.com/resources/articles/painless-threading.html

Solution 4:

First of all in the Above code, you need to start the Thread using this.

t.start();

you can also try below code,

new Thread ( new Runnable() 
{
        public void run()
        {
           // Place your Intent Code here
        }
}.start();

Post a Comment for "Run Progress Bar While Switching Activity"