MapView搞砸了我的片段交易?

时间:2016-11-03 21:58:29

标签: android android-fragments android-mapview

我有一个带有mapview的片段。我可以从操作栏打开另一个片段(称之为listfragment),它工作正常。但是如果我旋转屏幕,然后尝试打开listfragment,它就不会加载,而是mapview按时闪烁(视图中的地图变为空白然后再次出现)。如果我尝试通过单击操作栏上的menuitem再次加载listfragment,则应用程序崩溃

  

java.lang.IllegalStateException:已添加片段:

加载片段的MainActivity的一部分:

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mNewTrackFragment = (NewTrackFragment) getFragmentManager()
                .findFragmentByTag(TAG_NEW_TRACK_FRAGMENT);
        if (mNewTrackFragment == null) {
            mNewTrackFragment = NewTrackFragment.newInstance();
        }
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .add(R.id.fragment_container, mNewTrackFragment)
                .commit();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_track_list) {
            if (null == mTrackListFragment) {
                mTrackListFragment = TrackListFragment.newInstance();
            }
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.fragment_container, mTrackListFragment, TAG_TRACK_LIST_FRAGMENT)
                    .addToBackStack(null)
                    .commit();

            return true;
        }

使用MapView的NewTrackFragment的一部分:

    public NewTrackFragment() {
}

public static NewTrackFragment newInstance() {
    NewTrackFragment fragment = new NewTrackFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_new_track, container, false);

    mTrackTitleTV = (TextView) view.findViewById(R.id.tv_track_title);
    mDistanceTV = (TextView) view.findViewById(R.id.tv_distance);
    mElapsedTimeTV = (TextView) view.findViewById(R.id.tv_elapsed_time);
    mSpeedTV = (TextView) view.findViewById(R.id.tv_speed);
    mAscentTV = (TextView) view.findViewById(R.id.tv_ascent);
    mDescentTV = (TextView) view.findViewById(R.id.tv_descent);
    mAltitudeTV = (TextView) view.findViewById(R.id.tv_altitude);
    mStartStopFab = (FloatingActionButton) view.findViewById(R.id.fab_startstop);
    mMapView = (MapView) view.findViewById(R.id.new_track_mapview);

    setupMapView(savedInstanceState);

    return view;
}

private void setupMapView(Bundle savedInstanceState) {
    mMapView.onCreate(savedInstanceState);
    mMapView.getMapAsync(this);
}

我在晚上的最佳时间搜索了一些关于它的信息,但没有发现任何类似的信息。

1 个答案:

答案 0 :(得分:0)

这里有一些问题。目前,您未在TAG_NEW_TRACK_FRAGMENT内的add调用中将onCreate作为标记传递。因此,您添加的NewTrackFragment实例没有关联的标记,因此对findFragmentByTag的调用将始终返回null。实际上,每次旋转设备时都会创建NewTrackFragment的新实例。这不好,因为FragmentManager的状态在设备轮换中保留,这意味着它仍然保留在您添加到它的每个Fragment。由于您无条件地拨打addFragmentManager最终将持有NewTrackFragment的多个实例。

话虽如此,您应该做的是在调用add时添加标记,只有在您知道add当前未持有某个实例的情况下才会调用FragmentManager NewTrackFragment

mNewTrackFragment = (NewTrackFragment) getFragmentManager()
                .findFragmentByTag(TAG_NEW_TRACK_FRAGMENT);
        if (mNewTrackFragment == null) {
            mNewTrackFragment = NewTrackFragment.newInstance();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .add(R.id.fragment_container, mNewTrackFragment, TAG_NEW_TRACK_FRAGMENT)
                    .commit();
        }

您可能也希望对TrackListFragment执行类似的操作:

if (id == R.id.action_track_list) {
            FragmentManager fragmentManager = getFragmentManager();
            mTrackListFragment = (TrackListFragment) fragmentManager.findFragmentByTag(TAG_TRACK_LIST_FRAGMENT);
            if (null == mTrackListFragment) {
                mTrackListFragment = TrackListFragment.newInstance();
                fragmentManager.beginTransaction()
                        .replace(R.id.fragment_container, mTrackListFragment, TAG_TRACK_LIST_FRAGMENT)
                        .addToBackStack(null)
                        .commit();
            }
}