简体   繁体   中英

OOP in java for Android studio app

Im new to java, although have about a years experience with python and programming in general. I somewhat understand object oriented programming although since just recently teaching myself java, I dont understand how/why a piece of code works. The code is for an android app created in android studio, where I am trying to learn how to create an app using the google maps API. The java code is this:

package com.example.harry.myapplication;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

As I understand it, this code creates the class, but I cant see where an object of the class is produced in order for the code to be run. All of my programming experience has come from python/procedural programming.

In addition to not fully understanding how this code works, if i was to call the method .getUiSettings(), what object would I call this on?

What you have is your class definition -- ie a set of properties and methods that belong to your object, in this case MapsActivity . When you go onto an Android device and open your app, the Android operating system reads your app's AndroidManifest.xml to determine which Activity is your app's main activity. (If you made this in Android Studio, I'm guessing that this activity was automatically designated as the main activity, and you shouldn't have to change anything.) The Android operating system creates an instance of that class, and then calls a specific sequence of methods to alert your object that certain things are happening.

getUiSettings() is a method of the GoogleMap object. In your activity's onCreate() method, you call getMapAsync() . This launches a background thread to download the map information from Google without blocking up your main thread. When that information has been fully received, onMapReady() is called with the prepared GoogleMap as the parameter. You then save this object for later use with

mMap = googleMap;

If you wanted to access the map's UI settings, you'd call the method on your map object

mMap.getUiSettings();

Android coding is very much based around asynchronicity and callbacks and can be a little confusing at first. I would agree with @Vucko that perhaps you should find an online course or a more experienced programmer to learn from.

The map object is created by the map fragment asynchronously and returned to the listener, your activity. And getUiSettings would be called on the GoogleMap object. As you can see in the docs it's a method of the object.

As mentioned in comments, maybe SO isn't the place for this question. I found out recently there is a code review site in the stack exchange network. Maybe it's more appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM