Android 第二次调用活动时,不会停止加载进度条

Android 第二次调用活动时,不会停止加载进度条,android,Android,我已经创建了一个欢迎页面(启动器)并包含了一个进度条。加载此进度条后(几秒钟内),将显示下一个活动。第一次,当应用程序运行时,几秒钟后,将显示下一个活动。现在,当我按“上一步”按钮时,将显示欢迎活动。但这次,几秒钟后,它现在像第一次一样调用下一个活动(使用意图)。如何解决这个问题? 代码是: import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivit

我已经创建了一个欢迎页面(启动器)并包含了一个进度条。加载此进度条后(几秒钟内),将显示下一个活动。第一次,当应用程序运行时,几秒钟后,将显示下一个活动。现在,当我按“上一步”按钮时,将显示欢迎活动。但这次,几秒钟后,它现在像第一次一样调用下一个活动(使用意图)。如何解决这个问题? 代码是:

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);
    }


}
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.widget.ProgressBar;
公共类WelcomeActivity扩展了AppCompatActivity{
进度条p;
私用布尔运算;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
p=(ProgressBar)findViewById(R.id.ProgressBar);
最终线程时间读取=新线程(){
@凌驾
公开募捐{
mbActive=true;
试一试{
int=0;
而(mbActive&&(等待<1000)){
睡眠(200);
如果(mbActive){
平均值+=200;
updateProgress(等待);
}
}
}捕捉(中断异常e){
}最后{
继续();
}
}
};
timerThread.start();
}
@凌驾
公共空间{
super.ondestory();
}
public void updateProgress(最终整数时间通过){
if(null!=p){
最终int进度=p.getMax()*时间通过/100;
p、 设定进度(进度);
}
}
继续公开无效(){
Intent intd=新的Intent(这个,MainActivity.class);
星触觉(intd);
}
}

您应该使用
设置可见性(View.GONE)/设置可见性(View.INVISIBLE)

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

如果尝试发送,则CalledFromErrorThreadException是一个常见错误 从UI线程外部向UI线程发送UI事件


为了你的撞车案。阅读

您应该使用
设置可见性(View.GONE)/设置可见性(View.INVISIBLE)

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

如果尝试发送,则CalledFromErrorThreadException是一个常见错误 从UI线程外部向UI线程发送UI事件


为了你的撞车案。阅读

关闭进度对话框

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

关闭进度对话框

public void onContinue() 
{
p.dismiss();
Intent intd=new Intent(this,MainActivity.class);
startActivity(intd);
}
试试这个:--- 我更改了简历上的代码,并在简历上保留了进度显示代码

 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();
    }
  }
公共类WelcomeActivity扩展了AppCompatActivity{
进度条p;
私用布尔运算;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
public void updateProgress(最终整数时间通过){
if(null!=p){
最终int进度=p.getMax()*时间通过/100;
p、 设定进度(进度);
}
}
继续公开无效(){
Intent intd=新的Intent(这个,MainActivity.class);
星触觉(intd);
}
@凌驾
受保护的void onResume(){
super.onResume();
showProgress();
}
私有void showProgress(){
p=(ProgressBar)findViewById(R.id.ProgressBar);
最终线程时间读取=新线程(){
@凌驾
公开募捐{
mbActive=true;
试一试{
int=0;
而(mbActive&&(等待<1000)){
睡眠(200);
如果(mbActive){
平均值+=200;
updateProgress(等待);
}
}
}捕捉(中断异常e){
}最后{
继续();
}
}
};
timerThread.start();
}
@凌驾
公共空间{
super.ondestory();
}
}
试试这个:--- 我更改了简历上的代码,并在简历上保留了进度显示代码

 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();
    }
  }
公共类WelcomeActivity扩展了AppCompatActivity{
进度条p;
私用布尔运算;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
public void updateProgress(最终整数时间通过){
if(null!=p){
最终int进度=p.getMax()*时间通过/100;
p、 设定进度(进度);
}
}
继续公开无效(){
Intent intd=新的Intent(这个,MainActivity.class);
星触觉(intd);
}
@凌驾
受保护的void onResume(){
super.onResume();
showProgress();
}
私有void showProgress(){
p=(ProgressBar)findViewById(R.id.ProgressBar);
最终线程时间读取=新线程(){
@凌驾
公开募捐{
mbActive=true;
试一试{
int=0;
而(mbActive&&(等待<1000)){
睡眠(200);
如果(mbActive){
平均值+=200;
updateProgress(等待);
}
}
}捕捉(中断异常e){
}最后{
继续();
}
}
};
timerThread.start();
}
@凌驾
公共空间{
super.ondestory();
}
}

请参见在onCreate方法中调用onContinue方法的@Akhil Reddy。。。。。所以,当你们点击后退按钮时,恢复上的活动被称为…..活动再次调用其创建,所以若你们想再次调用第二个活动,请在OnResumeU plz pro中输入创建…线程代码