0

I have created a welcome page(launcher) and included a progress bar.next activity will be displayed after this progress bar loading(for few seconds).For the first time,when the app is run,after few seconds the next activity is displayed.Now,when i press back button,welcome activity is being displayed.But this time,after few seconds,it is now calling the next activity(using Intent) like the first time.How to resolve this? The code is:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;

public class WelcomeActivity extends AppCompatActivity {
    ProgressBar p;
    private boolean mbActive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        p = (ProgressBar) findViewById(R.id.progressBar);
        final Thread timerThread = new Thread() {
            @Override
            public void run() {
                mbActive = true;
                try {
                    int waited = 0;
                    while(mbActive && (waited < 1000)) {
                        sleep(200);
                        if(mbActive) {
                            waited += 200;
                            updateProgress(waited);
                        }
                    }
                } catch(InterruptedException e) {
                } finally {
                    onContinue();
                }
            }
        };
        timerThread.start();

    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    public void updateProgress(final int timePassed) {
        if(null != p) {
            final int progress = p.getMax() * timePassed / 100;
            p.setProgress(progress);
        }
    }

    public void onContinue() {
        Intent intd=new Intent(this,MainActivity.class);
        startActivity(intd);
    }


}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Akhil Reddy
  • 371
  • 1
  • 6
  • 26
  • see @Akhil Reddy you call onContinue method in onCreate method..... so when you click back button activity on resume is called .....activity is calling its create again so if you want to call second activity again put on create ...thread code in onResume – santoXme Jun 05 '17 at 08:49
  • could u plz provide me onResume sample code.I am a beginner.Thanks in advance. – Akhil Reddy Jun 05 '17 at 09:19
  • i cant post code in comment because char length check didn't allow me so i added an answer to it..... – santoXme Jun 05 '17 at 09:20
  • how to add onCreate thread in onResume()? – Akhil Reddy Jun 05 '17 at 09:20
  • i post an answer go though it ...if have any dought plz ask – santoXme Jun 05 '17 at 09:21

3 Answers3

1

You should use setVisibility(View.GONE);/setVisibility(View.INVISIBLE) .

public void onContinue() 
    {
    p.setVisibility(View.GONE);
    Intent intd=new Intent(this,MainActivity.class);
    startActivity(intd);
    }

FYI

CalledFromWrongThreadException is a common error if you tried to send UI events to the UI thread from outside the UI thread.

For your crash case . Read runOnUiThread

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • if i am addinh this p.setVisibility(View.GONE), the app is getting crashed. – Akhil Reddy Jun 05 '17 at 08:52
  • @AkhilReddy add `onContinue();` method in `try` block – IntelliJ Amiya Jun 05 '17 at 08:56
  • FATAL EXCEPTION: Thread-4 Process: com.akhil.aesqrgen, PID: 5849 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. – Akhil Reddy Jun 05 '17 at 08:57
  • @AkhilReddy Yeah . Its a general . See https://stackoverflow.com/questions/14978052/only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-views-on-a and https://stackoverflow.com/a/14114546/3395198 – IntelliJ Amiya Jun 05 '17 at 08:59
  • could u plz provide me sample code.I am a beginner.Thanks in advance – Akhil Reddy Jun 05 '17 at 09:06
  • Read https://stackoverflow.com/questions/5499389/android-cancelling-progressbar-caught-exception-android-view-viewrootcalled – IntelliJ Amiya Jun 05 '17 at 09:17
  • @AkhilReddy as @IntelliJ Amiya you should run the `p.setVisibility(View.Gone)` as for the crash this happens because you are modifying the ***UI*** in a Thread thats not the UI thread in this case your `timerThread`, so what you need to do is to to request the UI thread and then manipulate the UI. You can find the answer in the link that was posted in the last comment. PS: You can only manipulate UI elements in the UI thread. – M090009 Jun 05 '17 at 09:23
  • yeah,understood now,Thanq – Akhil Reddy Jun 05 '17 at 09:25
  • @M090009 Indeed .#Reddy Move ahead – IntelliJ Amiya Jun 05 '17 at 09:25
1

Try this :--- i change the code on keep the progress show code in on resume

 public class WelcomeActivity extends AppCompatActivity {

    ProgressBar p;
    private boolean mbActive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_welcome);
    }

    public void updateProgress(final int timePassed) {
      if (null != p) {
        final int progress = p.getMax() * timePassed / 100;
        p.setProgress(progress);
      }
    }

    public void onContinue() {
      Intent intd = new Intent(this, MainActivity.class);
      startActivity(intd);
    }

    @Override
    protected void onResume() {
      super.onResume();
      showProgress();
    }

    private void showProgress() {
      p = (ProgressBar) findViewById(R.id.progressBar);
      final Thread timerThread = new Thread() {
        @Override
        public void run() {
          mbActive = true;
          try {
            int waited = 0;
            while (mbActive && (waited < 1000)) {
              sleep(200);
              if (mbActive) {
                waited += 200;
                updateProgress(waited);
              }
            }
          } catch (InterruptedException e) {
          } finally {
            onContinue();
          }
        }
      };
      timerThread.start();
    }

    @Override
    public void onDestroy() {
      super.onDestroy();
    }
  }
santoXme
  • 802
  • 5
  • 20
0

dismiss the Progress dialog

public void onContinue() 
{
p.dismiss();
Intent intd=new Intent(this,MainActivity.class);
startActivity(intd);
}
Preet
  • 13
  • 4