我有一个带有ActivityMapFragment,可以使用Activity以编程方式将其添加到FragmentTransaction中:

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实例,并设置其设置,设置监听器并对其进行处理。一切正常。

然后,当用户完成 map 操作后,会触发AsyncTask显示ProgressDialog,执行一些操作,将不同的 fragment 放入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中进行操作,然后可能决定返回 map 。按下返回按钮, map 再次弹出。这是 map 变得迟钝的时候。它上的国家/城市名称加载速度非常慢,在移动 map 上非常滞后...而且我不知道为什么,在将MapFragment弹出时我什么也不做。

有趣的是,它已得到修复,例如,按下主屏幕按钮,然后再次返回到应用程序...

我究竟做错了什么?

谢谢您的任何想法。

最佳答案

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

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) {使其始终在旋转时创建新 fragment 时,我可以看到问题,这显然是错误的,但这带来了一些怀疑。

您可以同时在内存中看到1个以上的MapFragment吗?尝试使用Eclipse Memory Analyzer(MAT)。

关于android - 从 fragment 弹出 fragment 后,Google Maps v2滞后,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16265346/

10-10 15:22