我創建了一個歡迎頁面(啓動程序)幷包含一個進度條。在此進度條加載後(幾秒鐘),將顯示進度條。對於第一個時間,當應用程序運行時,幾秒鐘後顯示下一個活動。現在,當我按回按鈕時,歡迎活動正在顯示。但是這一次,幾秒鐘後,它現在調用下一個活動(使用意圖)像第一次。如何解決這個問題? 的代碼是:當第二次調用活動時,進度條加載沒有停止
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);
}
}
看到@Akhil雷迪你在onCreate方法中調用onContinue方法..... 所以當你點擊恢復按鈕時,活動就會被調用.....活動正在調用它的再次創建,所以如果你想再次調用第二個活動放在creat e ...線程代碼在onResume – santoXme
可以üPLZ提供我onResume示例code.I是一個初學者。提前感謝。 –
我不能在評論中發佈代碼,因爲字符長度檢查不允許我所以我加了一個答案..... – santoXme