2016-08-30 34 views
0

在我的代碼,我調用一個新的活動,但在執行時新Activity在當前Activity開始這個代碼剪斷舊人會不會暫停開始新的活動不會暫停當前

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
     float x = event.getX(); 
     float y = event.getY(); 
     float[] userCordinates = new float[2]; 

     userCordinates[0] = x; 
     userCordinates[1] = y; 
     userSequence.add(userCordinates); 
     for (int r = 0; r < copySeq.size(); r++) { 
      ImageView iv = (ImageView) (findViewById((Integer) copySeq.get(r))); 
      int[] loc = new int[2]; 
      iv.getLocationOnScreen(loc); 
      float xRangeMax = iv.getRight(); 
      float xRangeMin = iv.getLeft(); 
      float yRangeMax = iv.getBottom(); 
      float yRangeMin = iv.getTop(); 

      Integer point = (Integer)copySeq.get(r); 

      if (x <= xRangeMax && x >= xRangeMin 
       && y <= yRangeMax && y >= yRangeMin) { 
       if(copyColor.get(r).equals("green")){ 
       Intent intent = new Intent(this, ChildLevel.class); 
    startActivity(intent); 
      } 
      break; 
     } 
    } 
} 

但應該在我回來時執行。例如。這個Activity應該完全暫停。

if (userSequence.size() >= finalSequence.size()) { 
     childLevel=false; 
     save(); 
     check(userSequence); 
     touchView.setEnabled(false); 
    } 
}  
return false; 

請問誰能告訴我我做錯了什麼?謝謝!

+0

第二個代碼片段是insi de ChildLevel活動? –

+0

@ Matias Elorriaga沒有第二個片段屬於第一個 –

回答

1

當您啓動ChildLevel活動時,當前(暫時稱爲MainActivity)暫停(調用onPause()方法)。

如果你想,當你回到MainActivity要執行的第二代碼片段,把這些代碼裏面onResume()方法MainActivity

編輯:所以,你只需要在你回去執行這段代碼到MainActivityChildLevel。您需要使用startActivityForResult()

MainActivity,而不是startActivity(),使用startActivityForResult()

Intent intent = new Intent(this, ChildLevel.class); 
startActivityForResult(i, 123); 

然後,在ChildLevel,當你想回去:

Intent returnIntent = new Intent(); 
setResult(Activity.RESULT_OK, returnIntent); 
finish(); 

最後,在MainActivity :

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == 123) { 
     if (resultCode == Activity.RESULT_OK){ 
      // the code you want to execute 
     } 
    } 
} 
+0

,但是當我將它放入onResume中時,ocde也會在活動從其他resons恢復時執行,例如在按下home按鈕之後,在這種情況下,不應該執行片段 –

+0

是的,你說「但是當我回來時應該執行」..你究竟想要什麼?請更清楚 –

+0

當我從 –

相關問題