开发者

Android Maps Back Button

I'm developing an application that shows a path on a map, determined by a KML file. Specifically, in the MapActivity that is starting the map:

public void onCreate(Bundle savedInstanceState) {
    super.onCreat开发者_如何学Pythone(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    Uri uri = Uri.parse("geo:0,0?q=http://urltokml");
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);
    mapIntent.setData(uri);

    startActivity(Intent.createChooser(mapIntent, kmlFile));
    finish();
}

The map loads fine and after a few seconds the path described by the KML shows up. The problem is, when I press the "Back" button, it does not return to the previous screen but instead just hides the KML overlay. If the "Back" button is pressed again, it will return to the previous screen.

Any ideas of how to solve this?


Depends on what version of the API you are using... in the later versions there is an "OnBackPressed" method that you can override in your activity to adjust the back behavior.


It's because you're starting your activity and loading a blank map

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

and then creating an Intent to launch a NEW map with the kml file

    Uri uri = Uri.parse("geo:0,0?q=http://urltokml");
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);
    mapIntent.setData(uri);

    startActivity(Intent.createChooser(mapIntent, kmlFile));
    finish();
}

So what's happening when you hit back is that it's leaving the second map (with the kml file) and returning to the first map (that is blank).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜