Search code examples
androidandroid-intentandroid-activityprogress-barprogress

Issue loading next activity


    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.initial_layout);

    // progress bar
    pb = (ProgressBar) findViewById(R.id.progressBar);
    pb.setVisibility(View.VISIBLE);
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (progressStatus < 100) {
                progressStatus += 1;
                pbHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        pb.setProgress(progressStatus);
                    }
                });
                try {
                    Thread.sleep(70);
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (pb.getProgress() >= 95) {
                Intent i1 = new Intent(initialActivity.this,startingActivity.class);
                startActivity(i1);
            }
        }
    }).start();
}

My goal is to automatically load next activity when the progress bar finishes loading without firing any other event , but i cant seem to do that. I guess somethings wrong with the Thread , i am a begginer. Any help appreciated.


Solution

  • Define custom interface which give you call back when progress is complete :

    public interface OnProgressFinishListener{
        public void onProgressFinish();
    }
    

    Use AsyncTask to update progress :

    public void startProgress(final OnProgressFinishListener onProgressFinishListener){
        new AsyncTask<Void,Integer,Integer>(){
          @Override
          protected Integer doInBackground(Void... params) {
              while (progressStatus < 100) {
                     progressStatus += 1;
                     publishProgress(progressStatus);
    
                     try {
                          Thread.sleep(70);
                     }catch (Exception e) {
                          e.printStackTrace();
                     }
              }
              return progressStatus;
          }
    
          @Override
          protected void onProgressUpdate(Integer... values) {
             super.onProgressUpdate(values);
             pb.setProgress(values[0]);
          }
    
          @Override
          protected void onPostExecute(Integer progress) {
             super.onPostExecute(progress);
             if(progress==100){
                onProgressFinishListener.onProgressFinish();
             }
          }
       }.execute();
    }
    

    How to implement custom interface :

    pb = (ProgressBar) findViewById(R.id.progressBar);
    pb.setVisibility(View.VISIBLE);
    startProgress(new OnProgressFinishListener() {
        @Override
        public void onProgressFinish() {
            pb.setVisibility(View.GONE);
            Toast.makeText(MainActivity.this,"Progress Finish",Toast.LENGTH_SHORT).show();
        }
     });