Fragment with MapView lags / stutters on loading - preloading?

I have a pretty simple snippet in my application that has two edittext (input address) fields as well as a MapView. When the fragment is loaded, it freezes for a short period of time - obviously when loading / bloating or initializing the MapView.

The following is currently happening in the onCreateView fragment:

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

    View v = inflater.inflate(R.layout.fragment_search_place, container, false);

    mMapView = (MapView) v.findViewById(R.id.mapview);
    mImageContinue = (ImageView) v.findViewById(R.id.img_continue);
    mEditFrom = (ClearableEditText) v.findViewById(R.id.clearedit_from);
    mEditTo = (ClearableEditText) v.findViewById(R.id.clearedit_to);
    mButtonSwitch = (ImageButton) v.findViewById(R.id.btn_swap);


    mMapView.onCreate(savedInstanceState);

    mMap = mMapView.getMap();
    MapsInitializer.initialize(getActivity());

    mEditFrom.addTextChangedListener(mEditTextWatcherFrom);
    mEditTo.addTextChangedListener(mEditTextWatcherTo);

    if(mInteractionListener != null) {
        String[] add = mInteractionListener.getAddresses();
        if(add != null) {
            mEditFrom.setText(add[0]);
            mEditTo.setText(add[1]);
        }
    }

    mButtonSwitch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // swap from and to.
            String tmp = mEditFrom.getText();
            mEditFrom.setText(mEditTo.getText());
            mEditTo.setText(tmp);
            tmp = null;
        }
    });

    mImageContinue.setOnClickListener(this);

    return v;
}

      

Basically I just go through my views and set up some listeners, check if the parent activity (mInteractionListener) has already stored the variables for the EditText fields and initializes the MapView.

Is there a fairly clean way to initialize an async MapView, or somewhere other than the UI thread? Currently, when performing a FragmentTransaction, the UI - especially the NavigationDrawer - freezes for a short period of time, which doesn't look good at all.

+3


source to share





All Articles