2011-07-18 76 views
0

我有一個奇怪的問題,真的不知道從哪裏開始調試。我有兩個活動,一個持有googlemap視圖,當用戶點擊視圖上的標記時,我開始一個新的活動,顯示該標記的詳細描述。如果我在背後的活動中擊中了後面的buttton,它會將我返回到地圖活動中,這一點非常好。但是,如果我點擊一個新的(或相同的)標記,我再次進入詳細的活動(所有罰款),如果我嘗試再次按下後退按鈕,我會返回到第一次點擊的詳細活動,我可以擊中再次返回並最終再次進入地圖活動。Android:後退按鈕表演奇怪

如果我繼續debugggin我可以得到高達10個活動,我要回推前我finaly獲取到地圖活動再次

究竟發生了什麼事? Android忘記歷史或由於我實施地圖活動而不是活動?

任何人有哪裏開始查找問題

這裏的想法而來的,是大量的代碼:

地圖活動

public class SpotGuideMapActivity extends MapActivity 
{ 
    protected MapView mapView; 
    protected MapController mapController; 
    protected List<Overlay> mapOverlays; 
    protected Drawable overlayIcon; 
    protected SpotGuideOverlay spotsOverlay; 
    protected MyLocationOverlay myLocOverlay; 

    protected ArrayList<SpotItem> _spots; 

    protected Button ButtonHome; 
    protected Button ButtonPreferences; 

    protected Intent _intent = null; 


    private SpotGuideDbAdapter _spotDbAdapter; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.spot_map); 
     _intent = getIntent(); 
     ActivityHelper.createInstance(this).setTopBarTitle("Spot kortet"); 

     ButtonPreferences = (Button)findViewById(R.id.btnPreferences); 
     ButtonPreferences.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 


       startActivity(new Intent(getApplicationContext(), GlobalPreferencesActivity.class));   
       overridePendingTransition(R.anim.slide_right_in,R.anim.slide_right_out); 
      } 
     }); 

     ButtonHome = (Button)findViewById(R.id.btnHome); 
     ButtonHome.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       startActivity(new Intent(getApplicationContext(), HomeActivity.class));   
       overridePendingTransition(R.anim.slide_left_in,R.anim.slide_left_out); 
      } 
     }); 


     //mapView.invalidate(); 
    } 

    @Override 
    protected void onResume() 
    { 
     super.onResume(); 
     if(_intent.hasExtra("latitude") && _intent.hasExtra("longitude")) 
     { 
      String latitude = _intent.getStringExtra("latitude"); 
      String longitude = _intent.getStringExtra("longitude"); 
      double lat = Double.parseDouble(latitude); 
      double lon = Double.parseDouble(longitude); 

      GeoPoint point = new GeoPoint((int)(lat * 1E6) , (int)(lon * 1E6)); 
      initMapView(point); 
     } 
     else 
     { 
      initMapView(null); 
     } 

     drawSpotMarkers(); 

    } 
    @Override 
    protected void onPause() 
    { 
     super.onPause(); 
     myLocOverlay.disableCompass(); 
     myLocOverlay.disableMyLocation(); 

    } 

    protected void initMapView(GeoPoint centerPoint) 
    { 

     mapView = (MapView)findViewById(R.id.spotGuideMap); 
     mapView.setBuiltInZoomControls(true); 
     mapController = mapView.getController(); 

     //current location overlayer 
     myLocOverlay = new MyLocationOverlay(this, mapView); 
     myLocOverlay.enableCompass(); 
     myLocOverlay.enableMyLocation(); 

     mapView.getOverlays().add(myLocOverlay); 

     GeoPoint point = null; 
     if(centerPoint == null) 
     { 
      point = myLocOverlay.getMyLocation(); 
      if(point == null) 
      { 
       point = new GeoPoint((int)(55.5616508394963 * 1E6) , (int)(12.563638687133789 * 1E6)); 
      } 
     } 
     else 
     { 
      point = centerPoint; 
     } 
     //get the last know location, be fresh so use last fix 
     //Location location = myLocOverlay.getLastFix(); 
     //GeoPoint locationPoint; 

     //locationPoint = new GeoPoint((int)(location.getLatitude() * 1E6) , (int)(location.getLongitude() * 1E6)); 

     mapController.animateTo(point); 
     mapController.setZoom(10);  
    } 

    protected void drawSpotMarkers() 
    { 
     mapOverlays = mapView.getOverlays(); 

     overlayIcon = getResources().getDrawable(R.drawable.map_icon_pink); 
     spotsOverlay = new SpotGuideOverlay(overlayIcon, this); 

     _spotDbAdapter = new SpotGuideDbAdapter(this).Open(); 
     _spots = _spotDbAdapter.getAllSpots(); 

     for (SpotItem spot : _spots) 
     { 
      double lat = Double.parseDouble(spot.getLatitude()); 
      double lon = Double.parseDouble(spot.getLongitude()); 

      GeoPoint point = new GeoPoint((int)(lat * 1E6) , (int)(lon * 1E6)); 

      OverlayItem overlayitem = new OverlayItem(point, spot.getSpotTitle(), Integer.toString(spot.getId())); 

      spotsOverlay.addOverlay(overlayitem); 
     } 


     mapOverlays.add(spotsOverlay); 


    } 


    @Override 
    protected boolean isRouteDisplayed() 
    { 
     return false; 
    } 

} 

詳細的活動

public class SpotGuideDescriptionActivity extends BaseActivity 
{ 
    private SpotGuideDbAdapter _spotDbAdapter; 

    protected Button ButtonHome; 
    protected Button ButtonPreferences; 

    protected Button ButtonBacktoMap; 

    protected TextView tvSpotTitle; 
    protected TextView tvSpotType; 
    protected TextView tvWindDirection; 
    protected TextView tvContent; 

    protected Intent _intent = null; 
    protected SpotItem item; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.spot_description); 
     _intent = getIntent(); 

     this.getActivityHelper().setTopBarTitle("Spot beskrivelse"); 

     tvSpotTitle = (TextView)findViewById(R.id.spotDescTitle); 
     tvSpotType = (TextView)findViewById(R.id.spotDescType); 
     tvWindDirection = (TextView)findViewById(R.id.spotDescWind); 
     tvContent = (TextView)findViewById(R.id.spotDescContent); 

     ButtonBacktoMap = (Button)findViewById(R.id.btnBackToMap); 
     ButtonBacktoMap.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       Intent intent = new Intent(getApplicationContext(), SpotGuideMapActivity.class); 
       intent.putExtra("latitude", item.getLatitude()); 
       intent.putExtra("longitude", item.getLongitude()); 
       startActivity(intent);      
       overridePendingTransition(R.anim.slide_left_in,R.anim.slide_left_out); 
      } 
     }); 

     ButtonPreferences = (Button)findViewById(R.id.btnPreferences); 
     ButtonPreferences.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 


       startActivity(new Intent(getApplicationContext(), GlobalPreferencesActivity.class));   
       overridePendingTransition(R.anim.slide_right_in,R.anim.slide_right_out); 
      } 
     }); 

     ButtonHome = (Button)findViewById(R.id.btnHome); 
     ButtonHome.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       startActivity(new Intent(getApplicationContext(), HomeActivity.class));   
       overridePendingTransition(R.anim.slide_left_in,R.anim.slide_left_out); 
      } 
     }); 

     init(); 
    } 

    private void init() 
    { 
     if(_intent.hasExtra("spot_id")) 
     { 
      int id = _intent.getIntExtra("spot_id", 0); 
      _spotDbAdapter = new SpotGuideDbAdapter(this).Open(); 

      item = _spotDbAdapter.getSpot(id); 
      tvSpotTitle.setText(item.getSpotTitle()); 
      tvSpotType.setText(item.getSpotType()); 
      tvWindDirection.setText(item.getWindDirections()); 
      tvContent.setText(item.getSpotDescription()); 
     } 
    } 
} 

然後我在我的OverlayItem中初始化水龍頭,就像這樣

public boolean onTap(int index) 
    { 
     OverlayItem item = mOverlays.get(index); 

     Intent descIntent = new Intent(currentContext.getApplicationContext(), SpotGuideDescriptionActivity.class); 
     descIntent.putExtra("spot_id", item.getSnippet()); 
     currentContext.startActivity(descIntent); 

     return false; 

    } 

它不是BackToMap按鈕,我使用,這是對所有手機

+0

你在哪裏開始'SpotGuideDescriptionActivity'? –

+0

thx指出它:)我做ontap在我的spotguideoverlay,擴展com.google.android.maps.ItemizedOverlay pumpin

回答

0

我還沒有與地圖工作的後退按鈕,所以我可能會關閉我的搖桿。但是,您在OnResume()中調用initMapView()的事實可能會創建兩個標記,一個在另一個之上(並且每當您返回時)。所以當你認爲你在錄一個標記時,你實際上是在貼上多個標記。

這只是我的頭頂,可以是完整的malarkey。

+0

烏斯特頭頂?做得好的隊友和thx隊友。像魅力一樣工作。 – pumpin

0

快速解決方案是@Override onBackPressed()打開地圖活動備份。不過,這可能會導致您必須執行導航整個應用程序。如果它只是地圖頁面和更多信息的疊加,就可以輕鬆只有這個被複制的網頁上:

@Override 
onBackPressed(){ 
    Intent i = new Intent(this, Activity.class); 
    startActivity(i); 
} 

然後在活動用的MapView

@Override 
onBackPressed(){ 
    moveTaskToBack(true); 
}