2012-09-23 94 views
0

我不清楚如何在Android中工作的程序。我最近两个月开始从事Android工作,同时也是Java初学者。所以,我正在努力发展和学习。这是我实现的代码片段,我不太清楚它是如何按照我的要求工作的。活动的生命周期是什么?

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(),我想绘制在地图上获得的点。我的意图是它像谷歌地图交通层。当我们拖动屏幕或放大/缩小时,我应该继续绘制。为此,我在TouchOverlay类中使用onZoom()方法中的相同getGPSPoints方法。

但是,当我第一次选择该选项并进行第一次放大/缩小操作时,它只是绘制一次。如果我需要绘制剩余部分,则必须按照当前实现再次单击该选项。这项活动如何运作,我应该拥有它?

+6

【活动生命周期(http://developer.android.com/ reference/android/app/Activity.html#ActivityLifecycle) – Lucifer

回答

1

您的onCreate方法在Android操作系统必须“创建”您的活动时调用。

这会在您的活动初始加载时发生,并且每当操作系统自动销毁您的活动或者您致电活动的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类相关的请你解释一下它的生命周期?我需要覆盖各种touchevent的所有数据。 – ChanChow

0

这一切都在下图中(其他地方dev的网站上找到):

Android Lifecycle

+0

嘿巴拉克,你能请我解释我的背景。 – ChanChow