Android loading is very slow / does not load at all at startup until you click on it

When starting a new activity of a card, the card loads very slowly and does not start loading until the screen is clicked.

The layout is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.android.momintuition.DirectionsActivity">

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:id="@+id/mapView"
        android:paddingTop="62px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        map:uiCompass="true"
        map:zOrderOnTop="true"
        map:uiZoomControls="true"
        android:background="#00000000" />
</RelativeLayout>

The new action is as follows:

public class DirectionsActivity extends AppCompatActivity implements OnMapReadyCallback {


    GoogleMap mMap; // Might be null if Google Play services APK is not available.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_directions);
        MapView mv = (MapView) findViewById(R.id.mapView);
        mv.onCreate(savedInstanceState);
        mMap = mv.getMapAsync(this);

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
         this.map = googleMap;
         CameraUpdate cameraUpdate =
                                CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
         map.animateCamera(cameraUpdate);
    }
}

I am new to Android. Am I missing something? Thanks!

+4
source share
2 answers

The problem in this case was that I did not implement the following methods (even if it was indicated in the documentation):

    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

This contradicts me why these methods may affect the map display, but it fixes the problem.

+8
source

TL DR These lines made my load on the map.

itemMap.onResume();
mapView.onEnterAmbient(null);

For people having the same problem:

RecyclerView, , racula, ArrayList MapView, mapView , .

Activity mapViewList . , .

    //In the Activity
    @Override
    protected void onResume() {
        super.onResume();
        for(MapView mapView : adapter.getMapViewList()){
            if (mapView != null){                   
                mapView.onResume();
            }              
        }
    }

, , "itemMap.onResume();" "mapView.onEnterAmbient(null);" , . Adapter

//RecyclerView Adapter and inside it the ViewHolder
class ReminderAdapter extends RecyclerView.Adapter<ReminderAdapter.MyViewHolder> {

    private ArrayList<MapView> mapViewList;

    @Override
    public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_layout, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ReminderAdapter.MyViewHolder holder, int position) {
        (...)
        holder.initializeMapView(new LatLng(getLat(), getLng()));
    }   

    ArrayList<MapView> getMapViewList(){
        return mapViewList;
    }     

    (...)

    class MyViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback{

    @Override
        public void onMapReady(GoogleMap googleMap) {
            MapsInitializer.initialize(context);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f));
            googleMap.addMarker(new MarkerOptions().position(latLng));
            this.googleMap = googleMap;
            itemMap.onResume();           //The methods
            itemMap.onEnterAmbient(null); // that made my map work
        }

        void initializeMapView(LatLng latLng){
            if(itemMap != null){             
                this.latLng = latLng;
                itemMap.onCreate(bundle);
                itemMap.getMapAsync(this);
                mapViewList.add(itemMap); // adds the MapView to the list
            }
        }
    }
}
+2

Source: https://habr.com/ru/post/1609979/


All Articles