Android-Mapbox方向更改丢失对MapboxMap的引用

时间:2018-12-01 21:06:51

标签: android android-fragments android-viewpager mapbox orientation-changes

不言自明-我在viewpager片段中有一个MapboxMap,如果屏幕旋转了,我将丢失对地图的引用(即,对于任何其他方法调用,map = null)。我已经实现了所有片段状态更改到最后,包括OnDestroyView,所以不确定发生了什么。以下是我的MapFragment.java的摘录。

@Override
public void onAttach(Context context) {
    Log.i(TAG,"Attaching Map");
    super.onAttach(context);
    mContext = context;
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    Log.i(TAG,"Detaching Map");
    super.onDetach();
    mListener = null;
}

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    return false;
}


/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}



@Override
public void onStart() {
    super.onStart();
    mMapView.onStart();
}

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

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onStop() {
    Log.i(TAG,"Stopping Map");
    super.onStop();
    mMapView.onStop();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

/*
@Override
public void onDestroy() {
    Log.i(TAG,"Destroying Map");
    super.onDestroy();
    //mMapView.onDestroy();
}
*/

@Override
public void onDestroyView() {
    super.onDestroyView();
    mMapView.onDestroy();
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    //mMapView.onSaveInstanceState(outState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);
    mMapView.onCreate(savedInstanceState);

0 个答案:

没有答案