2014-03-14 139 views
0

在此活動中,我使用Runnable旋轉圖像,而Button被按下。 問題是,當我終止整個應用程序後,當我再次啓動我的應用程序時,此活動會提前。我瞭解到,當回調被錯誤地刪除時會發生這種情況。關閉處理程序,刪除回調,無法殺死活動

我的問題:如何修復此代碼以正確刪除所有回調並避免在錯誤時刻重新開啓活動。

public class MatchTimeActivity extends Activity { 

Button MatchTime; 
TextView Header1; 
ImageView Face; 
Timer myTimer; 
private Handler matchHandler; 
private Runnable matchAction; 
public final static String match_int_value1="com.planis.matchthetime.match_int_value1"; 
public final static String match_int_value2="com.planis.matchthetime.match_int_value2"; 

@Override 
public void onBackPressed() { 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_match_time); 
    MatchTime = (Button) findViewById(R.id.MatchTimeButton); 
    Header1 = (TextView) findViewById(R.id.MatchTimeHeader); 
    Typeface custom_font2 = Typeface.createFromAsset(getAssets(), "fonts/font1.ttf"); 
    MatchTime.setTypeface(custom_font2); 
    Header1.setTypeface(custom_font2); 

    matchHandler = new Handler(); 
    matchAction = new Runnable() { 
     @Override public void run() { 
      Face = (ImageView) findViewById(R.id.MatchTimeImage); 
      Face.setRotation(Face.getRotation()+9);      
      matchHandler.postDelayed(this, 25); 
     } 
    }; 

    ListenTo(); 

} 

public void onStop(){ 
    super.onStop(); 
    finish(); 
} 
public void ListenTo(){ 
    MatchTime.setOnTouchListener(new OnTouchListener(){ 
     long time_start=0; 
     long time_end=0; 

     @Override 
     public boolean onTouch(View view, MotionEvent event) { 

      if (event.getAction() == MotionEvent.ACTION_DOWN) {     
       time_start=System.currentTimeMillis(); 
       matchHandler.post(matchAction); 

       return true; 
      } 
      if (event.getAction() == MotionEvent.ACTION_UP) {     
       time_end=System.currentTimeMillis(); 
       long TimeCounted=time_end-time_start; 
       matchHandler.removeCallbacksAndMessages(null); 
       matchHandler = null; 
       SaveAndSend(TimeCounted); 
       return true; 
      } 
      return false; 
     } 

    }); 
} 



public void SaveAndSend(long TimeCountedlong){ 
    int TimeCountedint=(int)(long) TimeCountedlong; 
    Intent intent = getIntent();            
    int TimeToMatchsas = intent.getIntExtra("display_int_value",0); 
    Intent intent2 = new Intent(this, ResultsActivity.class); 
     intent2.putExtra("match_int_value1", TimeToMatchsas); 
     intent2.putExtra("match_int_value2", TimeCountedint); 
    startActivity(intent2); 
    finish(); 
} 

}

+0

您的應用程序還有其他活動嗎?如果此活動是清單中的主要活動,那麼從您的代碼的外觀開始,您的代碼將被調用,只會創建一個新的處理程序和可運行的程序。那麼你是否正在尋找你的應用程序開始而不啓動此活動?當你說終止應用程序,你切換到不同的應用程序,所以這個應用程序仍然在後面的堆棧?只是尋求更多的澄清。 – dudebrobro

+0

這不是主要活動。在我的主要活動中,我使用finish()重寫了onBackPressed方法來關閉應用程序。然而,在測試中,當我按下「返回」按鈕時,應用程序不會關閉,並且此活動不必要地前進。當我按下主頁按鈕並離開我的應用程序時,當我再次打開時,此活動會提前顯示,而不是主要活動。有時第二次嘗試一切正常,按下後退按鈕後應用程序關閉。 – user3348059

回答

2

您需要掛鉤onPause()和清理您的回調之前,你的活動暫停,並最終銷燬。

@Override public void onPause() { matchHandler.removeCallbacksAndMessages(null); super.onPause(); }