我创建了一个欢迎页面(启动器)并包含了一个进度条。在此进度条加载后(几秒钟)将显示下一个活动。这是第一次,当应用程序运行时,几秒后,下一个显示活动。现在,当我按下后退按钮时,正在显示欢迎活动。但是这一次,几秒钟后,它正在调用下一个活动(使用Intent),就像第一次一样。如何解决这个问题? 代码是:
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);
}
}
答案 0 :(得分:1)
您应该使用 setVisibility(View.GONE);/setVisibility(View.INVISIBLE)
。
public void onContinue()
{
p.setVisibility(View.GONE);
Intent intd=new Intent(this,MainActivity.class);
startActivity(intd);
}
<强> FYI 强>
如果您尝试发送,则CalledFromWrongThreadException是一个常见错误 从UI线程外部到UI线程的UI事件。
对于你的崩溃案例。阅读 runOnUiThread
答案 1 :(得分:1)
试试这个:--- 我改变代码保持进度显示代码在简历
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();
}
}
答案 2 :(得分:0)
关闭进度对话框
public void onContinue()
{
p.dismiss();
Intent intd=new Intent(this,MainActivity.class);
startActivity(intd);
}