Mapbox onStop in fragment

I have a map-map in my fragment.

public class Map extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public MapView mapView;

public Map() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment Map.
 */
// TODO: Rename and change types and number of parameters
public static Map newInstance(String param1, String param2) {
    Map fragment = new Map();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    Mapbox.getInstance(getContext(), getString(R.string.access_token));
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

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

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

    mapView = (MapView) rootView.findViewById(R.id.mapview);
    mapView.onCreate(savedInstanceState);

    mapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(MapboxMap mapboxMap) {

            mapboxMap.setStyleUrl("mapbox://styles/some_style_url");

        }
    });

    return rootView;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

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

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}

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

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

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

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

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

@Override
public void onStop() {
    super.onStop();
    mapView.onStop();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
}
}

      

I get an error when I rotate my device

04-25 14:37:48.677 27329-27329/com.travip E/AndroidRuntime: FATAL EXCEPTION: main
                                                        Process: com.travip, PID: 27329
                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.travip/com.travip.MainActivity}: java.lang.IllegalStateException: MapView#onStop() was not called. You must call this method from the parent {@link Activity#onStop()} or {@link Fragment#onStop()}.
                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
                                                            at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4170)
                                                            at android.app.ActivityThread.access$1000(ActivityThread.java:163)
                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                            at android.os.Looper.loop(Looper.java:148)
                                                            at android.app.ActivityThread.main(ActivityThread.java:5585)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                         Caused by: java.lang.IllegalStateException: MapView#onStop() was not called. You must call this method from the parent {@link Activity#onStop()} or {@link Fragment#onStop()}.
                                                            at com.mapbox.mapboxsdk.maps.MapView.onDestroy(MapView.java:279)
                                                            at layout.Map.onDestroy(Map.java:163)
                                                            at android.support.v4.app.Fragment.performDestroy(Fragment.java:2434)
                                                            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1442)
                                                            at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                            at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:753)
                                                            at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                            at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                            at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                            at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                            at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                            at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
                                                            at android.app.Activity.performStart(Activity.java:6295)
                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4170at android.app.ActivityThread.access$1000(ActivityThread.java:163at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368at android.os.Handler.dispatchMessage(Handler.java:102at android.os.Looper.loop(Looper.java:148at android.app.ActivityThread.main(ActivityThread.java:5585at java.lang.reflect.Method.invoke(Native Method) 
                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620
      

I have implemented onStop () in a Fragment. Or should I implement onStop () in MainActivity (parent activity)? And if so, how? Thank you for your advice.

EDIT: Logcat said onStop () should be called from parent activtiy, so I added this to MainActivity

@Override
protected void onStop() {
    super.onStop();
    Nearby nearby = new Nearby();
    nearby.mapView.onStop();
}

      

This threw a null pointer exception, but I thought it might be helpful for you.

+3


source to share


1 answer


I finally figured out how to fix this.
Since Mapbox 5.x Mapbox.getInstance(getContext(), getString(R.string.access_token));

has to be called from the activity that the fragment is in, not in the onCreate of the fragment. So I put it from onCreate of my fragment inside onCreate of my activity and it works.
Hope this helps you.



-1


source







All Articles