0

So, I have an activity that holds tabs, each tab has a fragment assigned to it, so you can navigate through the fragments using the tabs. In two of these tabs I have to load a map (with different information on each of them, different pins, etc) the problem is it only loads the first one, I'm using this code for assigning each tab the fragment desired (this is an adapter working fine).

@Override
public Fragment getItem(int pos) {
    switch (pos) {
        case 0:
            return new SearchFragment();
        case 1:
            return new ParkFragment();
        case 2:
            setActiveSessionsFragment();
            return mActiveSessionsFragment;
        case 3:
            return new ChoosePaymentMethodFragment();
        default:
            return new ParkFragment();
    }
}

being the case 0 and 2 the ones loading the map, if I substitute any of these for any other fragment the one with the map works fine, but if leave both the app crashes. In both fragments I'm loading the map in a FrameLayout like this one:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/map_container_search_tab"
    android:background="@color/gray"></FrameLayout>

And I create them programmatically by:

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

    //Create a new map fragment
    SupportMapFragment mapFragment = SupportMapFragment.newInstance();
    getFragmentManager().beginTransaction().add(R.id.map_container_search_tab, mapFragment).commit();
    mapFragment.getMapAsync(this);
}

However I have no onDestroy method nor onDestroyView, I've tried to implement these by what I've seen in other responses but it's not working and I have tried to solve this by myself like for a whole week now. This is the method I tried but with no luck

public void onDestroyView() {
    super.onDestroyView();
    if (mMap != null) {
        getFragmentManager().beginTransaction()
                .remove(getFragmentManager().findFragmentById(R.id.map_container))
                .commit();
    }

This is the logcat when it crashes:

05-16 15:09:33.540 24971-24971/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.parso.mobile.android, PID: 24971
                                               java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
                                                   at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1489)
                                                   at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1507)
                                                   at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
                                                   at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
                                                   at com.parso.mobile.android.fragments.SearchFragment.onActivityCreated(SearchFragment.java:108)
                                                   at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1970)
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                                                   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                   at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
                                                   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
                                                   at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:946)
                                                   at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2910)
                                                   at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
                                                   at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
                                                   at com.parso.mobile.android.MainActivity.onParkingSessionCreated(MainActivity.java:240)
                                                   at com.parso.mobile.android.fragments.ConfirmSessionFragment$CreateParkingSessionTask.onPostExecute(ConfirmSessionFragment.java:213)
                                                   at com.parso.mobile.android.fragments.ConfirmSessionFragment$CreateParkingSessionTask.onPostExecute(ConfirmSessionFragment.java:189)
                                                   at android.os.AsyncTask.finish(AsyncTask.java:632)
                                                   at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5538)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:958)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:753)
Diego Rivera
  • 403
  • 1
  • 5
  • 19
  • Take a look at the `Solution using TabLayout from the support library` section of this answer: http://stackoverflow.com/a/32579020/4409409 – Daniel Nugent Apr 19 '16 at 20:59
  • yes, using a fragment that extends SupportMapFragment helps it work, however in that fragment I'm not being able to use the Context, I dont know why – Diego Rivera May 16 '16 at 21:20
  • Inside any Fragment, use `getActivity()` to get the Activity Context. – Daniel Nugent May 16 '16 at 23:19

0 Answers0