每当我从一个选项卡切换到另一个选项卡时,都会再次调用onCreate方法

时间:2013-06-20 12:21:51

标签: android google-maps android-tabhost

我正在制作一个我正在使用TabHost的Android应用程序。在第一个选项卡(名为“MAP VIEW”)中,我在其中显示地图,第二个选项卡用于listview(名为“DETAILS VIEW”)。我的问题是当我从MAP VIEW选项卡切换到DETAILS VIEW选项卡然后返回MAP VIEW选项卡onCreate方法再次被调用并且map再次开始加载。我想要的是,当我返回到MAP VIEW选项卡时,它将保存我第一次离开它的行为。

这是我的Java类,它显示了Tabs。

private static final String MAP_TAB_TAG = "MAP VIEW";
    private static final String DETAILS_TAB_TAG = "DETAILS VIEW";
    private TabHost tHost;
    private ListView mList;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        tHost = getTabHost();

        Intent intent;

        intent = new Intent(Second.this, MapsView.class);

        tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("MAP VIEW").setContent(intent));

        intent= new Intent(Second.this, DetailsView.class);

    tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("DETAILS VIEW").setContent(intent));

这是我的MAP VIEW Java代码(每次我回到这个课程时都会调用onCreate这个类)

public class MapsView extends FragmentActivity implements OnMapClickListener,
        OnMapLongClickListener, OnCameraChangeListener,
        OnInfoWindowClickListener , OnMarkerClickListener, LocationListener, ConnectionCallbacks, OnConnectionFailedListener{

    private GoogleMap mMap;
    private static final String LAT = "lat";
    private static final String LONG = "lng";
    Geocoder coder;
    String address;
    private LocationClient mLocationClient;

    private static final LocationRequest REQUEST = LocationRequest.create()
              .setInterval(5000)         // 5 seconds
              .setFastestInterval(16)    // 16ms = 60fps
              .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps_view);
        setUpMapIfNeeded();
        setUpLocationClientIfNeeded();
        mLocationClient.connect();

        System.out.println("Oncreate");

        coder = new Geocoder(this);
    }

    @Override
    protected void onResume() {
        super.onResume();


    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (mLocationClient != null) {
              mLocationClient.disconnect();
            }
    }

    private void setUpLocationClientIfNeeded() {
        if (mLocationClient == null) {
          mLocationClient = new LocationClient(
              getApplicationContext(),
              this,  // ConnectionCallbacks
              this); // OnConnectionFailedListener
        }
      }

    public void showMyLocation(View view) {
        if (mLocationClient != null && mLocationClient.isConnected()) {
          String msg = "Location = " + mLocationClient.getLastLocation();
          Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
        }
      }

     @Override
      public void onConnected(Bundle connectionHint) {

         Location location = mLocationClient.getLastLocation();
            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 14);
            mMap.animateCamera(cameraUpdate);
        mLocationClient.requestLocationUpdates(
            REQUEST,
            this);  // LocationListener
      }

    private void setUpMapIfNeeded() {

        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();  
        }
        if (mMap != null) {
            setUpMap();
        }
    }

    private void setUpMap() {
        // mMap.addMarker(new MarkerOptions().position(new LatLng(0,
        // 0)).title("Marker"));
        mMap.setMyLocationEnabled(true);
        mMap.setOnMapClickListener(this);

        mMap.setOnMapLongClickListener(this);
        mMap.setOnCameraChangeListener(this);
        mMap.setOnInfoWindowClickListener(this);
        mMap.setOnMarkerClickListener(this);
    }

    @Override
    public void onMapClick(LatLng point) {

    }

    @Override
    public void onMapLongClick(LatLng point) {

        try {
            List<Address> list = coder.getFromLocation(point.latitude,
                    point.longitude, 1);
            if (list.size() > 0) {
                mMap.clear();
                address = "";
                if (list.get(0).getAddressLine(0) != null) {

                    address += list.get(0).getAddressLine(0) + " ";
                }

                if (list.get(0).getAddressLine(1) != null) {

                    address += list.get(0).getAddressLine(1) + " ";
                }

                if (list.get(0).getAddressLine(2) != null) {

                    address += list.get(0).getAddressLine(2) + " ";

                }



            mMap.addMarker(new MarkerOptions()
                        .position(point)
                        .title(address)
                        .icon(BitmapDescriptorFactory
                                .fromResource(R.drawable.traffic_pin)));

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    @Override
    public void onCameraChange(final CameraPosition position) {

    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        // TODO Auto-generated method stub
        String lat = "" + marker.getPosition().latitude;
        String lng = "" + marker.getPosition().longitude;

        startActivity(new Intent(MapsView.this, Tab.class).putExtra(LAT, lat)
                .putExtra(LONG, lng).putExtra("title", marker.getTitle()).putExtra("address", address));
    }

这是显示结果的LogCat

06-20 17:04:42.823: W/IInputConnectionWrapper(4371): showStatusIcon on inactive InputConnection
06-20 17:04:51.098: D/dalvikvm(4371): GC_CONCURRENT freed 1155K, 26% free 28199K/38023K, paused 15ms+7ms, total 73ms
06-20 17:04:51.098: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 48ms
06-20 17:04:51.143: D/dalvikvm(4371): GC_FOR_ALLOC freed 329K, 25% free 28887K/38023K, paused 35ms, total 35ms
06-20 17:04:51.263: D/SensorManager(4371): registerListener :: handle = 0  name= K3DH Acceleration Sensor delay= 200000 Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)b)a-,(
06-20 17:04:52.933: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 4 mFalseSizeCnt:0
06-20 17:04:53.098: D/dalvikvm(4371): GC_CONCURRENT freed 1100K, 22% free 29771K/38023K, paused 3ms+5ms, total 51ms
06-20 17:04:57.068: D/Result(4371): s
06-20 17:04:57.073: D/Result:(4371): s
06-20 17:04:57.263: D/SensorManager(4371): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,)b)a-,(
06-20 17:04:57.263: D/Sensors(4371): Remain listener = Sending .. normal delay 200ms
06-20 17:04:57.263: I/Sensors(4371): sendDelay --- 200000000
06-20 17:04:57.263: D/SensorManager(4371): JNI - sendDelay
06-20 17:04:57.263: I/SensorManager(4371): Set normal delay = true
06-20 17:04:57.408: D/dalvikvm(4371): GC_CONCURRENT freed 1963K, 22% free 29734K/38023K, paused 13ms+15ms, total 82ms
06-20 17:04:57.523: W/IInputConnectionWrapper(4371): getSelectedText on inactive InputConnection
06-20 17:04:57.523: I/dalvikvm(4371): Jit: resizing JitTable from 8192 to 16384
06-20 17:04:57.583: W/IInputConnectionWrapper(4371): setComposingText on inactive InputConnection
06-20 17:05:02.578: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
06-20 17:05:02.698: D/AbsListView(4371): Get MotionRecognitionManager
06-20 17:05:02.708: D/ProgressBar(4371): setProgress = 0
06-20 17:05:02.708: D/ProgressBar(4371): setProgress = 0, fromUser = false
06-20 17:05:02.708: D/ProgressBar(4371): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000
06-20 17:05:02.818: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:02.818: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:04.203: D/response(4371): {"data":[{"id":"1","device_id":"123","lat":"33.6605","lng":"73.0779","address":"Street 95 Islamabad Pakistan ","traffic_level":"3","details":"ghsjxjdd","date":"2013-06-18","time":"07:34 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/c48f2201f748550f9442b326c9a00d60.jpg"},{"id":"2","device_id":"123","lat":"33.6606","lng":"73.0769","address":"Street 96 Islamabad Pakistan ","traffic_level":"1","details":"zuaiffhfyd","date":"2013-06-18","time":"07:44 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/5be821ec69ea233c11be8c7448646613.jpg"},{"id":"3","device_id":"123","lat":"33.6602","lng":"73.0772","address":"Street 95 Islamabad Pakistan ","traffic_level":"4","details":"yfgghtcggcfg","date":"2013-06-18","time":"07:59 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/fb801c3f5872049029bd9e54598d4dcb.jpg"},{"id":"4","device_id":"123","lat":"33.6621","lng":"73.078","address":"Street 98 Islamabad Pakistan ","traffic_level":"1","details":"new testing","date":"2013-06-20","time":"06:37 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/e091977ec224a60e6eb160d74799d591.jpg"},{"id":"5","device_id":"123","lat":"33.6686","lng":"73.0695","address":"Street 30 Islamabad Pakistan ","traffic_level":"1","details":"fydjgjyfmfhx","date":"2013-06-20","time":"08:03 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/5a909c814bf9e89352073a3d76138b4d.jpg"},{"id":"6","device_id":"123","lat":"33.6707","lng":"73.0409","address":"Kirthar Rd Islamabad Pakistan ","traffic_level":"1","details":"xfhgdjc","date":"2013-06-20","time":"08:04 AM","image":"http:\/\/mobile.s3technology.net\/traffic\/images\/123\/d5e52d50ec1d78a664b6d4aeb039b080.jpg"}]}
06-20 17:05:04.453: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:04.453: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:06.038: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 21 mFalseSizeCnt:0
06-20 17:05:09.058: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 10 mFalseSizeCnt:0
06-20 17:05:09.788: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
06-20 17:05:10.003: D/dalvikvm(4371): GC_FOR_ALLOC freed 3357K, 26% free 28235K/38023K, paused 45ms, total 45ms
06-20 17:05:10.003: I/dalvikvm-heap(4371): Grow heap (frag case) to 30.033MB for 1836252-byte allocation
06-20 17:05:10.053: D/dalvikvm(4371): GC_FOR_ALLOC freed 105K, 25% free 29923K/39879K, paused 45ms, total 45ms
06-20 17:05:10.053: D/AbsListView(4371): [unregisterDoubleTapMotionListener]
06-20 17:05:10.053: I/MotionRecognitionManager(4371):   .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,)b/fa*(
06-20 17:05:10.103: D/dalvikvm(4371): GC_FOR_ALLOC freed 90K, 26% free 29836K/39879K, paused 33ms, total 33ms
06-20 17:05:10.108: I/dalvikvm-heap(4371): Grow heap (frag case) to 32.958MB for 3264524-byte allocation
06-20 17:05:10.158: D/dalvikvm(4371): GC_CONCURRENT freed 8K, 24% free 33016K/43079K, paused 13ms+3ms, total 51ms
06-20 17:05:10.333: W/SurfaceView(4371): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false
06-20 17:05:10.368: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:10.368: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:11.578: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:11.578: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:12.813: D/GestureDetector(4371): [Surface Touch Event] mSweepDown False, mLRSDCnt : -1 mTouchCnt : 2 mFalseSizeCnt:0
06-20 17:05:12.853: D/AbsListView(4371): Get MotionRecognitionManager
06-20 17:05:12.863: I/System.out(4371): Oncreate
06-20 17:05:12.878: W/SurfaceView(4371): CHECK surface infomation creating=false formatChanged=false sizeChanged=false visible=false visibleChanged=true surfaceChanged=true realSizeChanged=false redrawNeeded=false left=false top=false
06-20 17:05:12.953: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:12.953: E/SpannableStringBuilder(4371): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-20 17:05:13.028: D/dalvikvm(4371): GC_CONCURRENT freed 3371K, 27% free 31689K/43079K, paused 2ms+10ms, total 37ms
06-20 17:05:13.028: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 30ms
06-20 17:05:13.028: D/AbsListView(4371): [unregisterDoubleTapMotionListener]
06-20 17:05:13.033: I/MotionRecognitionManager(4371):   .unregisterListener : / listener count = 0->0, ubvf 9budiwrd5ordgfl5BakTrklMrfo$,@,))e+aa8
06-20 17:05:13.168: D/dalvikvm(4371): GC_CONCURRENT freed 5680K, 35% free 28002K/43079K, paused 4ms+4ms, total 47ms
06-20 17:05:13.168: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 15ms
06-20 17:05:14.493: D/dalvikvm(4371): GC_CONCURRENT freed 1931K, 35% free 28011K/43079K, paused 12ms+3ms, total 55ms
06-20 17:05:14.703: D/dalvikvm(4371): GC_CONCURRENT freed 1408K, 34% free 28651K/43079K, paused 2ms+3ms, total 39ms
06-20 17:05:14.703: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 30ms
06-20 17:05:14.703: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 32ms
06-20 17:05:14.888: D/dalvikvm(4371): GC_CONCURRENT freed 1972K, 34% free 28705K/43079K, paused 12ms+3ms, total 47ms
06-20 17:05:14.888: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 17ms
06-20 17:05:15.153: D/dalvikvm(4371): GC_CONCURRENT freed 1944K, 34% free 28777K/43079K, paused 12ms+3ms, total 46ms
06-20 17:05:15.153: D/dalvikvm(4371): WAIT_FOR_CONCURRENT_GC blocked 27ms
06-20 17:05:42.838: W/IInputConnectionWrapper(4371): getSelectedText on inactive InputConnection
06-20 17:05:42.843: W/IInputConnectionWrapper(4371): setComposingText on inactive InputConnection

任何帮助将不胜感激 提前致谢

1 个答案:

答案 0 :(得分:1)

最后我找到了问题的解决方案:-)我在我的TabHost类中做了逻辑错误看看代码

private static final String MAP_TAB_TAG = "MAP VIEW";
    private static final String DETAILS_TAB_TAG = "DETAILS VIEW";
    private TabHost tHost;
    private ListView mList;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        tHost = getTabHost();

        Intent intent;


 intent = new Intent(Second.this, MapsView.class);

        tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("MAP VIEW").setContent(intent));

        intent= new Intent(Second.this, DetailsView.class);

    tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("DETAILS VIEW").setContent(intent));

在&#34; DETAILS VIEW&#34;部分我给了tHost.newTabSpec(MAP_TAB_TAG),但MAP_TAB_TAG是用于MAP VIEW标签的静态变量,DETAILS_TAB_TAG用于DETAILS VIEW标签。所以,我的逻辑错误是我在给予

tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("MAP VIEW").setContent(intent)); tHost.addTab(tHost.newTabSpec(MAP_TAB_TAG).setIndicator("DETAILS VIEW").setContent(intent));

你可以看到tHost.newTabSpec(MaP_TAG_TAG)用于两个标签,这是我的错误,现在我已经解决了这个问题,感谢您的评论并以任何方式回复