8

我有一个Activitywith a ,我使用 a 以编程MapFragment方式添加到:ActivityFragmentTransaction

private static final String MAP_FRAGMENT_TAG = "map";
private MapFragment mapFragment = null;

...

protected void onCreate(Bundle savedInstanceState) {

    ...

    mapFragment = (MapFragment) getFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);
    if (mapFragment == null) {
        mapFragment = MapFragment.newInstance();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.fragment_wrapper, mapFragment, MAP_FRAGMENT_TAG);
        fragmentTransaction.commit();
    }

    ...

}

标准方式。然后我GoogleMap从 中获取实例mapFragment并设置它的设置,设置监听器,用它做一些事情。一切正常。

然后当用户完成地图时,AsyncTask触发 an 以显示 a ProgressDialog,执行一些操作,将不同的片段放入fragment_wrapper并再次关闭ProgressDialog

private class GetFlightsTask extends AsyncTask<Double, Void, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    // the activity context has been passed to the AsyncTask through its constructor
    loadingFlightsSpinner = new ProgressDialog(context);
    // setting the dialog up
    loadingFlightsSpinner.show();
}

@Override
protected String doInBackground(Double... params) {
    // some pretty long remote API call
    // (loading a JSON file from http://some.website.com/...)
}

@Override
protected void onPostExecute(String flightsJSON) {
    super.onPostExecute(flightsJSON);
    // here I do stuff with the JSON and then I swtich the fragments like this
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    FlightsFragment fragment = new FlightsFragment();
    fragmentTransaction.replace(R.id.fragment_wrapper, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    loadingFlightsSpinner.dismiss();
}

一切仍然正常。用户在地图中做了一些事情,FlightsFragment然后可能决定返回地图。按下后退按钮,地图再次弹出。这是地图变得滞后的时候。它上面的国家/城市名称加载非常缓慢,它在移动地图时严重滞后......我不知道为什么,我不做任何关于弹出的事情MapFragment

有趣的是,它会在例如按下主页按钮然后再次返回应用程序时得到修复......

我究竟做错了什么?

谢谢你的任何想法。

4

3 回答 3

1

只有按下后退按钮才会滞后?

如果那是问题尝试阻止后退按钮或使其退出应用程序,请尝试以下代码:

    @Override
public void onBackPressed(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("You wanna leave the aplication?").setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();

}

或者试试这个代码,它是一种将地图片段放入另一个片段(嵌套地图片段)的方法,它在几周前对我有用:

Java 类:

public class Yourfragment extends Fragment {

    private MapView mMapView;
    private GoogleMap mMap;
    private Bundle mBundle;

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

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }

        mMapView = (MapView) inflatedView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        setUpMapIfNeeded(inflatedView);

        return inflatedView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

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

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

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

XML:

<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />

将此代码放在执行后:

View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);

try {
    MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
    // TODO handle this situation
}

mMapView = (MapView) inflatedView.findViewById(R.id.map);
mMapView.onCreate(mBundle);
setUpMapIfNeeded(inflatedView);

return inflatedView;

并在 oncreateview 上调用 assynctask

试试这个:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    //Call assyncTask
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mBundle = savedInstanceState;
}

private void setUpMapIfNeeded(View inflatedView) {
    if (mMap == null) {
        mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}

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

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

@Override
public void onDestroy() {
    mMapView.onDestroy();
    super.onDestroy();
}
private class GetFlightsTask extends AsyncTask<Double, Void, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    // if I remove the next line, everything gets fixed
    loadingFlightsSpinner.show();
}

@Override
protected String doInBackground(Double... params) {
    // some pretty long remote API call
    // (loading a JSON file from http://some.website.com/flights?...)
    // works fine
    String flightsJSON = loadJSON("flights?flyFrom=CZ&to=...");
}

@Override
protected void onPostExecute(String flightsJSON) {
    super.onPostExecute(flightsJSON);
    loadingFlightsSpinner.dismiss();
    // here I do stuff with the JSON and then replace the fragment
    dohardwork()
}
public view dohardwork(){
    View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);

    try {
        MapsInitializer.initialize(getActivity());
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO handle this situation
    }

    mMapView = (MapView) inflatedView.findViewById(R.id.map);
    mMapView.onCreate(mBundle);
    setUpMapIfNeeded(inflatedView);

    return inflatedView;
}
于 2013-05-06T11:33:00.693 回答
1

我运行了一个简单的测试:

public class MapFragmentOnBackStackExample extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_fragment_on_back_stack_example);

        FragmentManager fm = getSupportFragmentManager();
        Fragment f = fm.findFragmentById(R.id.fragment_container);
        if (f == null) {
            f = SupportMapFragment.newInstance();
            FragmentTransaction transaction = fm.beginTransaction();
            transaction.add(R.id.fragment_container, f);
            transaction.commit();
        }
    }

    public void onAddFragmentClick(View view) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.fragment_container, new MyFragment());
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public static class MyFragment extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            TextView textView = new TextView(getActivity());
            textView.setText("MyFragment: " + hashCode());
            return textView;
        }
    }
}

并且看不到任何问题。

当评论离开它总是在旋转时创建新片段时,我可以看到问题if (f == null) {,这显然是错误的,但这会带来一些怀疑。

你能在内存中同时看到超过 1 个 MapFragment 吗?尝试使用 Eclipse 内存分析器 (MAT)。

于 2013-05-07T22:15:19.460 回答
1

我已经通过在's方法ProgressDialog的末尾而不是在方法的开头取消已经解决了它。AsyncTaskdoInBackground()onPostExecute()

这有点奇怪,因为我实际上认为我不应该在doInBackground()方法中触摸 UI 中的东西......如果有人想详细说明一下,我很高兴知道它为什么会这样工作。

于 2013-05-08T14:28:39.100 回答