3

I have a rather simple Fragment in my application which has two edittext fields (address input) as well as a MapView. Upon loading the fragment, it freezes for a short period of time - apparently when loading / inflating or initializing the MapView.

In the fragment's onCreateView, the following happens currently:

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;
}

I'm basically just finding my views and setting some listeners, check if the parent activity (mInteractionListener) already has saved variables for the EditText fields - and initialize the MapView.

Is there a rather clean way to initialize the MapView async or somewhere, except on the UI thread? Currently when committing the FragmentTransaction, the UI - especially the NavigationDrawer - freezes for a short period of time, which does not look good at all.

damian
  • 2,001
  • 20
  • 38
  • I have also had this problem. The only workaround I have found is to delay the addition of the map a bit (in your case letting the drawer close) http://stackoverflow.com/questions/26178212/first-launch-of-activity-with-google-maps-is-very-slow/26463681#comment41570886_26463681 – cYrixmorten Dec 03 '14 at 09:42

0 Answers0