2014-10-08 22 views
0

在我的應用程序中,我使用導航抽屜從一個片段移動到另一個片段。片段中的onResume方法不起作用

如果我從about_us片段移動到另一個片段,那麼如果我再次回到相同的位置,活動不會恢復,相反,應用程序會不幸地收起來。

誰能告訴我什麼是我的代碼中的問題,請告訴我解決方案來解決這個問題。

public class AboutUsFragment extends Fragment { 

public AboutUsFragment(){} 

private GoogleMap gmap; 

static final double latitude = 33.12615; 
static final double longitude = 80.21932; 

TextView aboutUs; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_about_us, container, false); 

    initilizeMap(); 

    return rootView; 

} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 
} 

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onActivityCreated(savedInstanceState); 

    View aboutUs = getActivity().findViewById(R.id.about_us_content_view); 

    try { 
     // Loading map 
     initilizeMap(); 

     //To enable compass 
     gmap.getUiSettings().setCompassEnabled(true); 

     //To locate the school - Creating Marker 
     MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("School Location"); 

     // adding marker 
     gmap.addMarker(marker); 

     //We assigned latitude and longitude in Coordinate 
     LatLng coordinate = new LatLng(latitude, longitude); 
     CameraUpdate schoolLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5); 
     gmap.animateCamera(schoolLocation); 

     CameraPosition cameraPosition = new CameraPosition.Builder() 
     .target(coordinate)   // Sets the center of the map to Mountain View 
     .zoom(17)     // Sets the zoom 
     .bearing(90)    // Sets the orientation of the camera to east 
     .tilt(30)     // Sets the tilt of the camera to 30 degrees 
     .build();     // Creates a CameraPosition from the builder 
     gmap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 

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

} 

private void initilizeMap() 
{ 
    if (gmap == null) { 
     gmap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

     // check if map is created successfully or not 
     if (gmap == null) { 
      Toast.makeText(getActivity(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show(); 

     } 
    } 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    initilizeMap(); 
} 
} 

而且我about_us.xml是

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<fragment 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.MapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="220dp" /> 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@+id/map" 
    android:layout_centerHorizontal="true" > 

    <RelativeLayout 
     android:id="@+id/RelativeLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/about_us_heading_View" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:text="@string/about_us" 
      android:paddingTop="5dp" 
      android:paddingLeft="10dp" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <TextView 
      android:id="@+id/about_us_content_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:text="@string/abouts_us_content" 
      android:paddingTop="55dp" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:textAppearance="?android:attr/textAppearanceMedium"/> 

    </RelativeLayout> 
</ScrollView> 



</RelativeLayout> 

正如我完全新的片段。有人請告訴我一個小白菜的方式來解決它。提前致謝。

+0

您的片段添加到堆棧中 – Pr38y 2014-10-08 08:50:15

+0

如何添加之前堆棧中? – IndependentDev 2014-10-08 08:55:32

+0

在將它添加到片段事務之前 – Pr38y 2014-10-08 09:03:29

回答

0

附加片段犯fragmnetTransaction

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    ft.addToBackStack(null); 
    ft.replace(R.id.content_frame, fragment).commit(); 
+0

由於我是android開發新手。我不明白這個片段交易。你能給我提供任何鏈接嗎?在添加到後臺堆棧之前將其添加到 – IndependentDev 2014-10-08 09:48:30

+0

。檢查http://www.learn2crack.com/2014/06/android-sliding-navigation-drawer-example.html – Pr38y 2014-10-08 11:03:55

+0

我補充說,但它只顯示錯誤。我想我沒有正確地添加上面的代碼,你給 – IndependentDev 2014-10-08 11:46:59

1
Fragment with mapFragment in layout crashes when you resume cause for a fragment onCreateView is called every time you visit it every time. 
Try to make your rootView static like below, 

public static View mRootView; 
    public static boolean mOnRangeChange= false; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     if(mRootView==null){ 
      mRootView = inflater.inflate(R.layout.fragment_map, container, false); 
     }else{ 
      ViewGroup parent = (ViewGroup) mRootView.getParent(); 
      if (parent != null) 
       parent.removeView(mRootView); 
     } 
     map_layout = (FrameLayout) mRootView.findViewById(R.id.map_layout); 
     mcategory = (Button) mRootView.findViewById(R.id.contact_categories); 
     mSharedpreferenceUtility = SharedpreferenceUtility.getInstance(getActivity()); 
     mcategory.setOnClickListener(this); 
     try { 
      initializeMap(mRootView); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return mRootView; 
    }