0

I have a fragment and in its layout I have a an area for two views. one is for list view. The other is for a map view.

the list view is simply a list view. the map view is a fragment that I commit once a button is clicked. So when I press that button for the first time, it takes around 2-3 seconds for it to replace the fragment in the dedicated view. In this time, all of the UI is just stuck. After that, the transition between list to map and vice versa is smooth.

how can that be overcome?

Layout's layout :

<RelativeLayout  

        ....

        <RelativeLayout
            android:id="@+id/mapViewLayout"
            android:layout_below="@id/restaurantListFragTxtNoResults"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="visible">

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/listViewLayout"
            android:layout_below="@id/restaurantListFragTxtNoResults"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:divider="@color/app_theme_color"
                android:dividerHeight="4px">
            </ListView>

             ...

        </RelativeLayout>   

     ...

</RelativeLayout>   

on button click to toggle to map view :

  mFragmentManager.beginTransaction()
                            .replace( R.id.mapViewLayout,
                                    new MapFragment() ).commit();
layoutView.findViewById(R.id.restViewLayout).setVisibility(View.GONE);
BVtp
  • 2,308
  • 2
  • 29
  • 68

1 Answers1

0

Try wrapping the fragment transaction with a handler.

new Handler().postDelayed(new Runnable() {
           @Override
           public void run() {
             mFragmentManager.beginTransaction()
                         .replace(R.id.mapViewLayout,
                                 new MapFragment()).commit();
           }
       }, 50);
fluffyBatman
  • 6,524
  • 3
  • 24
  • 25
  • See if it helps increasing up the delay 50 to 100 or 200. – fluffyBatman Jan 02 '17 at 12:08
  • unfortunately it didn't help either.. it only moved now more on the X axis (cause I have a sliding animation to left) before it got frozen again – BVtp Jan 02 '17 at 12:18
  • Move more on the x-axis smoother than previous means the delay is actually helping up. Would you give a spin by increasing the delay to 1000 or 1500? – fluffyBatman Jan 02 '17 at 12:21
  • but the freezing is not prevented, it just happens a little bit later .. that's not helpful to a user – BVtp Jan 02 '17 at 12:24
  • On a second thought, since you incorporates google map in your app, it's likely that google play service will take some time to initialize at first lunch. There is a hack to prevent the delay from kick in: http://stackoverflow.com/a/28133951/2709580 – fluffyBatman Jan 02 '17 at 12:29
  • didn't help.. I think it's not the map initializing that's to blame for the UI unresponsiveness but rather the transaction committing itself.. – BVtp Jan 02 '17 at 12:48