2012-09-23 2 views
0

안드로이드에서 작동하는 프로그램이 너무 명확하지 않습니다. 지난 두 달 동안 Android에서 작업하기 시작했으며 자바 초보자입니다. 그래서 나는 개발하고 배우기 위해 최선을 다하고 있습니다. 다음은 구현 한 코드 조각이며 내 요구 사항에 따라 작동 방식이 명확하지 않습니다.활동의 수명주기 란 무엇입니까?

activity{ 
    onCreate(){ 
      /* here i am using google maps api and trying to plot the current location*/     
     OverlayItem overlayItem1 = new OverlayItem(ourLocation,"Our Location","Position"); 
     CustomPinpoint custom1 = new CustomPinpoint(d, Activity.this); 
      custom1.insertPinpoint(overlayItem1); 
      overlayList.add(custom1);    
       controller.animateTo(ourLocation); 
     } 
    private class TouchOverlay extends com.google.android.maps.Overlay{ 
      public boolean onTouchEvent(MotionEvent event, MapView map){ 
       onZoom(); 
     } 
     } 
    public boolean onCreateOptionsMenu(Menu menu){} 
    public boolean onOptionsItemSelected(MenuItem item){ 
     case.X: 
     getGPSPoints();//Here i will be getting some gps points from stored database 
     // and I would like to plot them all on the map. 
     TouchOverlay touchOverlay = new TouchOverlay(); 
    overlayList.add(touchOverlay); 
    } 
    onPause(){ 
      super.onPause(); 
    lm.removeUpdates(this); 
    } 
    onResume(){ 
      super.onResume(); 
    lm.requestLocationUpdates(towers, 500, (float) 0.5, this); 
    } 
    onLocationChanged(Location l) { 
    // TODO Auto-generated method stub 
    clearmap(); 
    lat = (int) (l.getLatitude()*1E6); 
    longi = (int) (l.getLongitude()*1E6); 
    GeoPoint ourLocation = new GeoPoint(lat, longi); 
    CustomPinpoint custom = new CustomPinpoint(d, TrafficMapActivity.this); 
      OverlayItem overlayItem = new OverlayItem(ourLocation,"Our location","Position"); 
      custom.insertPinpoint(overlayItem); 
      overlayList.add(custom); 
     } 
     } 

내 질문 할 때 onLocationChanged 메서드가 호출되고 또한 onTouchEvent 방법?

getGPSPoints()으로 메소드를 만들었으므로 획득 한 점을 맵에 표시하고 싶습니다. 내 의도는 구글 맵 트래픽 레이어와 같다. 화면을 드래그하거나 확대/축소 할 때 계속해서 그려야합니다. 이를 위해 클래스의 onZoom() 메서드 내에서 동일한 getGPSPoints 메서드를 사용하고 있습니다.

처음 옵션을 선택하고 첫 번째 확대/축소 작업을 수행 할 때 한 번 그립니다. 나머지를 그릴 필요가 있으면 현재 구현에 따라 옵션을 다시 클릭해야합니다. 이 활동은 어떻게 작동합니까?

+6

[활동 수명주기 (http://developer.android.com/ reference/android/app/Activity.html # ActivityLifecycle) – Lucifer

답변

1

귀하의 onCreate 메소드는 Android OS가 활동을 "생성"해야 할 때마다 호출됩니다.

이것은 활동의 초기로드시, 그리고 OS가 자발적으로 활동을 파괴 할 때마다 또는 활동의 finish() 메소드를 호출 할 때 발생합니다.

onCreate 메서드 다음에 onStart이라는 다른 Activity 메서드가 나옵니다.

활동이 사용자에게 표시되면 호출됩니다.

onLocationChangedonTouchEvent 구현과 관련하여이 두 가지 유형의 메소드는 객체에 설정된 수신기에 의해 실행됩니다.

예를 들어 onLocationChanged은지도 수신기가 위치가 변경되었다고 판단 할 때마다 실행됩니다.

onTouchEvent은 사용자가 터치 이벤트를 수신하면 언제든지 실행됩니다.

onResume 메서드는 Activity 클래스에 속하지만 다른 시간에 호출 되더라도 onCreate과 유사합니다.

특히 액티비티가 전면 집중 뷰가 아닌 경우 항상 이 호출됩니다.

onResume 메서드는 의 반대입니다. 액티비티 뷰가 이제 화면의 포커스 뷰일 때 호출됩니다.

http://developer.android.com/training/basics/activity-lifecycle/pausing.html

+0

확인.onCreate는 앱이 실행될 때 한 번만 실행됩니다. – ChanChow

+0

아니요, finish() 메서드를 호출 한 경우 또는 뷰의 포커스가 맞지 않고 VM이 더 많은 메모리를 위해이를 파기하는 경우에도 호출 될 수 있습니다. – mrres1

+0

내가 만든 TouchOverlay 클래스와 관련하여 나에게 라이프 사이클을 설명해 주시겠습니까? 나는 다양한 touchevents에 대한 모든 데이터를 오버레이해야합니다. – ChanChow

0

그것은 아래 그림에있는 모든 (다른 장소 중 dev에 웹 사이트에) 있어요 :

Android Lifecycle

+0

안녕하세요, Barak, 제 상황을 설명해 주시겠습니까? – ChanChow