3 Replies - 191 Views - Last Post: 18 November 2018 - 07:57 AM

#1 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

How to organize my code to fit the Java convention

Posted 18 November 2018 - 06:31 AM

I'm beginning with Java (And Android), and I have a simple code that displays my current GPS location on the screen.
Now the entire code is written inside a single file (Main Activity). But in the tutorials I watch, they always separate certain code from the main code so that it looks more organized.

My code looks like this:

public class MainActivity extends AppCompatActivity {
private FusedLocationProviderClient mFusedLocationClient;
private LocationCallback mLocationCallback;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        //
    } else {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback,
                null /* Looper */);
    }

    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            if (locationResult == null) {
                return;
            }
            for (Location location : locationResult.getLocations()) {
                // Update UI with location data
                TextView tv = (TextView) findViewById(R.id.tv);
                Double latDouble = location.getLatitude();
                String latString = latDouble.toString();
                tv.setText(latString);
            }
        };
    };
}


Please help me manage the code so that all the location-finding-related code is in a separate file, and then somehow call it from the Main activity. I just can't get it to work when I try to organize it myself.

How would you move the location code and have a shorter code on the Main activity thus making it look organized?
Do I need to write it in a different class and then use "extend"? Or "import"?
I'm confused, that's why a real example on my code would greatly help.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: How to organize my code to fit the Java convention

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3617
  • View blog
  • Posts: 16,593
  • Joined: 20-September 08

Re: How to organize my code to fit the Java convention

Posted 18 November 2018 - 06:44 AM

You could separate the LocationCallback into another class. Pass the TextView in its constructor

Don't use mX as instance variable names in Java - that's really from the C++ world. Java uses camel-cased names such as locationCallback

This post has been edited by g00se: 18 November 2018 - 06:44 AM
Reason for edit:: typo

Was This Post Helpful? 1
  • +
  • -

#3 Foobarer   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 269
  • Joined: 28-March 17

Re: How to organize my code to fit the Java convention

Posted 18 November 2018 - 06:57 AM

Assuming I moved LocationCallback to a separate class. How do I call it from the Main Activity?
Do I need to use extend keyword on the Main Activity?
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3617
  • View blog
  • Posts: 16,593
  • Joined: 20-September 08

Re: How to organize my code to fit the Java convention

Posted 18 November 2018 - 07:57 AM

You create the callback at line 25. It wouldn't be any different with what i recommend (apart from passing the TextView in the ctor)

Quote

Do I need to use extend keyword on the Main Activity?

No. Why would you think that?

This post has been edited by g00se: 18 November 2018 - 07:57 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1