UI Hang while replacing fragment from setOnInfoWindowClickListener interface method of Google Map

左心房为你撑大大i 提交于 2019-12-23 17:49:12

问题


I am using google map in my application and display markers.

Now on click of marker window I want to replace the current fragment with other fragment.

for (StoreData store : comp.getArrStoreList()) {
                LatLng l = new LatLng(Double.parseDouble(store.getLat()),
                        Double.parseDouble(store.getLng()));

                MarkerOptions marker = new MarkerOptions()
                        .position(l)
                        .title(store.getName())
                        .snippet(store.getCity())
                        .icon(BitmapDescriptorFactory
                                .defaultMarker(BitmapDescriptorFactory.HUE_AZURE));

                Marker mrkr = mapCompanyList.addMarker(marker);

                CompanyStoreData companyStoreData = new CompanyStoreData();

                companyStoreData.setStoreData(store);

                companyStoreData.setCompanyId(comp.getId());

                listStoreData.put(mrkr, companyStoreData);


                mapCompanyList
                .setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker marker) {

                        try {

                            StoreDetailsFragment storeDetails = new StoreDetailsFragment();
                            Bundle b = new Bundle();

                            b.putSerializable(UserDefaults.STORE_OBJECT,
                                    listStoreData.get(marker).getStoreData());
                            b.putString("StoreAppID", listStoreData.get(marker).getCompanyId());

                            storeDetails.setArguments(b);

                            ((BaseTabbarContainer) companyListFragment.getParentFragment()).replaceFragment(
                                    storeDetails, true); // here my UI is hang

                        } catch (Exception e) {
                            Log.e("Exception", "Exception :: " + e.getMessage());
                        }

                    }
                });
            }

I am creating one hashmap which has <marker,storedata> marker as a key and storedata (my custom class object) as a value.

on onInfoWindowClick I am getting instance of store data on basis of key of hashmap(). It is working well but

((BaseTabbarContainer) companyListFragment.getParentFragment()).replaceFragment(
                                        storeDetails, true);  

Here my UI is getting hang. What is the issue


回答1:


I resolved the issue . if any one facing this issue please refer this.

What I done is create one handler to run the code snippet after some delay and return from the onInfoWindowClick method after calling handler.

mapCompanyList
                .setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker marker) {

                        final Marker mark = marker;
                        try {

                            new Handler().postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    callStore(mark);
                                }
                            }, 100);


                            return;
                        } catch (Exception e) {
                            Log.e("Exception", "Exception :: " + e.getMessage());
                        }

                    }
                });

callStore

    private void callStore(Marker marker) {
        StoreDetailsFragment storeDetails = new StoreDetailsFragment();
        Bundle b = new Bundle();

        b.putSerializable(UserDefaults.STORE_OBJECT,
                listStoreData.get(marker).getStoreData());
        b.putString("StoreAppID", listStoreData.get(marker).getCompanyId());

        storeDetails.setArguments(b);

        ((BaseTabbarContainer) CompanyListFragment.this.getParentFragment()).replaceFragment(
                storeDetails, true);

    }

and It worked;

But I still don't get why it is getting hang though and why it is resolved after returning from onInfoWindowClick method while running code in post delay?if any one have any idea please share.




回答2:


I was also facing this problem, for me I solved by stop using fragmentManager.executePendingTransactions(), if I use this line to replace fragment then its just HANG, but without this line its working without any problem. hope it will help for someone who will face similar problem. thanks.



来源:https://stackoverflow.com/questions/22985984/ui-hang-while-replacing-fragment-from-setoninfowindowclicklistener-interface-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!