2014-03-14 91 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(); }