2017-01-26 128 views
1

我有一個需要看到3秒的入口布局。如果我啓動應用程序,它保持3秒和activity更改,這沒關係,但問題是,如果我通過智能手機的背面回到這個activity,而我使用的應用程序avtivity不會在3秒後更改。我是新來的,找不到問題?你可以幫我嗎 ?由於如果我重新開始活動,如何重新啓動倒數計時器?

public class A1CoverPage extends AppCompatActivity { 

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

     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setCustomView(R.layout.a0action_bar); 

     new CountDownTimer(3000, 1000) { 

      public void onTick(long millisUntilFinished) { 

       EditText mTextField = (EditText) findViewById(R.id.editText); 
       mTextField.setText(" Loading... "); 

       ImageView mImageView = (ImageView) findViewById(R.id.imageView); 
       mImageView.setImageResource(R.drawable.amblem);  
      } 

      public void onFinish() { 

       Intent intent = new Intent(A1CoverPage.this, A2ExperimentCalibration.class); 
       startActivity(intent); 

      } 
     }.start();  
    } 
+0

如果你知道如何通過代碼重新啓動它,那麼你應該把它放在'onResume'方法中 – Roljhon

回答

1

onCreate將只執行一次,當創建您的活動,但是當你回過頭來此活動,則onCreate將不再執行。

所以overrideonstart方法Activity和移動你的代碼中onstart

EditText mTextField;  
ImageView mImageView; 
// ^^^^ declare views references 

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

    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setCustomView(R.layout.a0action_bar);  

    mTextField = (EditText) findViewById(R.id.editText); 
    mImageView = (ImageView) findViewById(R.id.imageView); 
    // ^^^ initialize views 
} 

@Override 
protected void onStart() { 
    super.onStart(); 

    new CountDownTimer(3000, 1000) {  
     public void onTick(long millisUntilFinished) {  
      mTextField.setText(" Loading... ");  
      mImageView.setImageResource(R.drawable.amblem); 
     } 

     public void onFinish() {  
      Intent intent = new Intent(A1CoverPage.this, A2ExperimentCalibration.class); 
      startActivity(intent);  
     } 
    }.start(); 
} 

改進:聲明中的視圖類和初始化它們onCreate,而不是在你的計時器每次內在他們匿名類

0

移動您的CountDownTimer裏面onStart()類似於此:

@Override 
    public void onStart() { 
     super.onStart(); 

     new CountDownTimer(3000, 1000) { 

     public void onTick(long millisUntilFinished) { 

      EditText mTextField = (EditText) findViewById(R.id.editText); 
      mTextField.setText(" Loading... "); 

      ImageView mImageView = (ImageView) findViewById(R.id.imageView); 
      mImageView.setImageResource(R.drawable.amblem); 

     } 

     public void onFinish() { 

      Intent intent = new Intent(A1CoverPage.this, A2ExperimentCalibration.class); 
      startActivity(intent); 

     } 
    }.start(); 
    }