在我的應用程序中,我使用導航抽屜從一個片段移動到另一個片段。片段中的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>
正如我完全新的片段。有人請告訴我一個小白菜的方式來解決它。提前致謝。
您的片段添加到堆棧中 – Pr38y 2014-10-08 08:50:15
如何添加之前堆棧中? – IndependentDev 2014-10-08 08:55:32
在將它添加到片段事務之前 – Pr38y 2014-10-08 09:03:29