Android:后退按钮表现奇怪

时间:2011-07-18 15:54:32

标签: android

我有一个奇怪的问题,真的不知道从哪里开始调试。我有两个活动,一个持有googlemap视图,当用户点击视图上的标记时,我开始一个新活动,显示该标记的详细描述。如果我在我的清除活动上击中后面的按钮它会让我回到地图活动,到目前为止一切都那么好。但是,如果我点击一个新的(或相同的)标记我再次进入详细活动(一切正常),如果我再次尝试按下后退按钮,我将返回到我第一次点击的详细活动,我可以点击再次回来,最后再次进入地图活动。

如果我保持debugggin,我可以获得最多10个活动,我必须在我最终再次进入地图活动之前重新开始

到底发生了什么事?因为我实现了地图活动而不是活动,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按钮,它是所有手机上的后退按钮

2 个答案:

答案 0 :(得分:0)

我没有使用地图,所以我可能不喜欢摇滚乐。但是你在OnResume()中调用initMapView()的事实可能是一个在另一个上面创建两个标记(并且每次返回时都是这样)。因此,当您认为自己正在录制一个标记时,实际上是在录制多个标记。

这只是我的头脑,可以是完全的malarkey。

答案 1 :(得分:0)

快速解决方案是@Override onBackPressed()打开地图活动备份。但是,这可能会导致您必须执行此操作才能导航整个应用程序。如果它只是地图页面和覆盖了更多信息,您可以轻松地在重复的页面上显示:

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

然后使用mapview

进行活动
@Override
onBackPressed(){ 
    moveTaskToBack(true);
}