2

我有一个主要活动,当按下此按钮时我有一个按钮,它显示地图。这很完美。

但是当我按下后退键时,我将再次进入我的主要活动,现在当我再次按下地图按钮时,它会显示整个世界地图。

我的意思是说它从头开始。

如何从 Back Stack 中完全删除我的地图活动并再次创建以再次显示我想要的地图?

或任何其他解决方案来做到这一点?

编辑:1

btn_find_atm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(NearByATM.this, Map.class);
            startActivityForResult(intent, request_map);

        }
    });

编辑:2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (isGooglePlayAvailable()) {

        expld = new ExpandablePlaceListData();

        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        // Getting the service from context and giving to location_manager
        location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);

        setContentView(R.layout.mapdemo);

        placesTask = new PlacesTask();

        getGoogleMap();

        getUserLocation();

        google_map.clear();

        Toast.makeText(this, "Latitude:" + lat + " Longitude:" + lang,
                Toast.LENGTH_LONG).show();

        drawMarker();

        sb = createUrl();

        placesTask.execute(sb);

    }

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent returnIntent = new Intent();
    setResult(RESULT_OK, returnIntent);
    this.finish();
}

编辑:3

private void getUserLocation() {

    if (location_manager != null) {
        provider = location_manager.getBestProvider(criteria, true);
        location = location_manager.getLastKnownLocation(provider);
        location_manager.requestLocationUpdates(provider,
                Map.MIN_TIME_BW_UPDATES,
                Map.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        if (location != null) {
            lat = location.getLatitude();
            lang = location.getLongitude();

        } else {

            Toast.makeText(Map.this, "Unable to idntify location",
                    Toast.LENGTH_LONG).show();

            lat = -22.00000;
            lang = 33.0000;

        }
    }
}

编辑:4

当我再次按下按钮加载地图时,LogCat 会显示类似的内容。

10-19 16:47:06.048: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:06.086: I/SurfaceTexture(4887): [unnamed-4887-1](this:0x5fa6d608,api:0) detachFromContext
10-19 16:47:06.087: I/BufferQueue(4887): [unnamed-4887-1](this:0x610cd580,api:0) consumerDisconnect
10-19 16:47:06.088: I/BufferQueue(4887): [unnamed-4887-1](this:0x610cd580,api:0) ~BufferQueue
10-19 16:47:06.090: D/dalvikvm(4887): threadid=22: exiting
10-19 16:47:06.096: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:06.097: D/dalvikvm(4887): threadid=22: bye!
10-19 16:47:08.782: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.783: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.785: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.853: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.853: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.854: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.864: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.864: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.865: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.866: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.866: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.867: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.874: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.882: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
4

2 回答 2

2

最后我所做的是,

@Override
public void onBackPressed() {
    super.onBackPressed();
    google_map.clear();
    location_manager.removeUpdates(this);
}

在活动结果上,我从后台堆栈中删除了所有活动。

Intent intent = new Intent(NearByATM.this, NearByATM.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

在主要活动中的清单文件上,我也从历史记录中删除了,

android:excludeFromRecents="true"
                android:noHistory="true"
于 2013-10-20T16:17:10.093 回答
1

您应该使用 LocationClient 和 Location 管理器,并且**您不必费心从后台堆栈中删除活动**或任何其他事情。让安卓系统为你处理。

实施GooglePlayServicesClient.ConnectionCallbacksGooglePlayServicesClient.OnConnectionFailedListener在您的活动中..

实施所有必要的方法。

LocationClient lc = new LocationClient(this, this, this);

public void onStart()
{
     super.onStart();
     lc.connect();
}

.....在实现方法的同一个类中......在
public void onConnected(Bundle b) {}

public void ondisConnected(Bundle arg0) {}

这是我看到你的代码能告诉你的最好的。由于您调用的方法太多,我看不到方法的实现。如果您实现此机制,则不必处理onBackPressed()或任何其他句柄。

一些代码....

public class Example extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

public void onCreate(Bundle arg0)
{
// Initialize LocationClient here. and do necessary things.
}

public void onStart()
{
    super.onStart();
    lc.connect();
}

public void onPause()
{
   // Remove object references and reset the map components if you want
}
public void onConnected(Bundle arg0)
{
     // This will be called when you use lc.connect();  and the connection is successful otherwise onConnectionfailed() if connection fails.
     // If connected then do necessary things on GOOGLE MAPS here.
     // Whatever your code is, put here what you have shown in the sample code.
     gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(YOUR_LATLNG_HERE, 15.00f));
     // The above statement will move your camera to the location with zooming and animation. 15.00f is the zoom level. you can change it.
}

public void onDisconnected(Bundle arg0)
{
     // Do on disconnect  
}

public void onConnectionFailed(ConnectionResult arg0)
{
    // Do something or leave blank if on connection failed.
}
}

此示例假定您的活动布局中有一个 mapfragment。
希望这可以帮助。

于 2013-10-19T11:53:59.437 回答