2nd activity crashing when trying to get current location due to 1st activity not stopping

58 Views Asked by At

I have one activity, activity A, that uses google maps API to display the user on the map and get location data. When the user clicks on a button they are taken to activity B in which it's supposed to get location data. My issue is that I have a method that returns Location so that I can get the latitude and longitude from the user in a form of a double but it returns null. Before the program crashes I have noticed that once the user switches to activity B, onPause() is called from activity A but never onStop() thus GoogleAPIClient disconnect is never called as well; I think this is where the issue lies. How can I call location data after I know that Activity A has stop and disconnected the gooleAPIClient? Do I need to disconnect the client if I switch activities?

Here is some of the code:

CurrentLocation.java

class CurrentLocation implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationListener {

    //some variables

    CurrentLocation(Activity activity, Context context, OnLocationChangedListener onLocationChangedListener) {
        Log.d(debug, "CurrentLocation has been called by " + activity);
        //code
    }

    synchronized void apiBuild() {
        Log.d(debug, "apiBuild has been called " + mActivity);


        if (mApiClient == null) {
            mApiClient = new GoogleApiClient.Builder(mContext)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        }
    }

    void apiStart() {
        Log.d(debug, "apiStart has been called by " + mActivity);
        mApiClient.connect();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Log.d(debug, "onConnected has been called by " + mActivity);
        //code
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d(debug, "onConnectionSuspended has been called by " + mActivity);
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.d(debug, "onConnectionFailed has been called by " + mActivity);
    }

    void apiStop() {
        Log.d(debug, mActivity + ": apiStop has been called by " + mActivity);
        mApiClient.disconnect();
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(debug, "onLocationChanged has been called by " + mActivity);

        //check permission

        Location lastKnownLocation = LocationServices.FusedLocationApi.getLastLocation(mApiClient);

         if (mApiClient != null)
             return lastKnownLocation; 
         else
             return null;

    }


    /************* Returns null in Activity B ***************/
    Location getCurrentLocation() {
        Log.d(debug, "getCurrentLocation has been called by " + mActivity);
        //code
    }
}

ActivityA.java:

public class ActivityA extends FragmentActivity implements
        View.OnClickListener,
        OnLocationChangedListener,
        OnMapReadyCallback {

    private CurrentLocation mCurrentLocation;
    private GoogleMap mGoogleMap;

    //more variables

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(debug, "onCreate has been Called!");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_world_play_hide);

        mCurrentLocation = new CurrentLocation(this, this, this);

        //Set Up Google Maps
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.hideMap);
        mapFragment.getMapAsync(this);

        //Link to XML
        Button actB = (Button) findViewById(R.id.activityB);

        mCurrentLocation.apiBuild();

        actB.setOnClickListener(this);
    }

    protected void onStart() {
        Log.d(debug, "onStart has been Called!");

        super.onStart();
        mCurrentLocation.apiStart();
        //code 
    }

    protected void onRestart() {
        Log.d(debug, "onRestart has been Called!");
        super.onRestart();
    }

    protected void onResume() {
        Log.d(debug, "onResume has been Called!");
        super.onResume();
        mCurrentLocation.apiStart();
    }

    protected void onPause() {
        Log.d(debug, "onPause has been Called!");
        super.onPause();
    }

    protected void onStop() {
        Log.d(debug, "onStop has been Called!");
        super.onStop();
        mCurrentLocation.apiStop();
    }

    protected void onDestroy() {
        Log.d(debug, "onDestroy has been Called!");
        super.onDestroy();
    }

    public void onClick(View v) {
        int i = v.getId();

        if (i == R.id.activityB)
            goB();
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        //code
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d(debug, "onLocationChanged has been called");

        //code;
    }


    private void goB() {
       intentBundle.setUserName(this, ActivityB.class, username);
    }
}

ActivityB.java:

public class ActivityB extends Activity implements
        SurfaceHolder.Callback,
        OnLocationChangedListener {

    private CurrentLocation mCurrentLocation;

    //variables

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(debug, "onCreate has been Called!");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_area);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        mCurrentLocation = new CurrentLocation(this, this, this);

        intentBundle = new IntentBundle(getIntent());
        username = intentBundle.getUserName();

        mCurrentLocation.apiBuild();

        setupLayout();
    }

    protected void onStart() {
        Log.d(debug, "onStart has been Called!");
        super.onStart();
        mCurrentLocation.apiStart();
    }

    protected void onRestart() {
        Log.d(debug, "onRestart has been Called!");
        super.onRestart();
    }

    protected void onResume() {
        Log.d(debug, "onResume has been Called!");
        super.onResume();
        mCurrentLocation.apiStart();
    }

    protected void onPause() {
        Log.d(debug, "onPause has been Called!");
        super.onPause();
    }

    protected void onStop() {
        Log.d(debug, "onStop has been Called!");
        super.onStop();
        mCurrentLocation.apiStop();
    }

    protected void onDestroy() {
        Log.d(debug, "onDestroy has been Called!");
        super.onDestroy();
    }


    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.d(debug, "surfaceChanged has been called!");
        //code
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.d(debug, "surfaceCreated has been called!");
        //code
        search();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d(debug, "surfaceDestroyed has been called!");
        //code
    }


    @Override
    public void onLocationChanged(Location location) {
        Log.d(debug, "onLocationChanged has been called");
        //code
    }

    private void setupLayout() {
        Log.d(debug, "setupLayout has been called!");

        //code
    }
    private void search() {
        Log.d(debug, "search has been called");

        /******* Crashes --> returns null ***********/
        latitude = mCurrentLocation.getCurrentLocation().getLatitude();
        longitude = mCurrentLocation.getCurrentLocation().getLongitude();
        //code
    }
}

Here is the logcat:

Activity A: onCreate has been Called!

CurrentLocation: CurrentLocation has been called by Activity A

CurrentLocation: apiBuild has been called by Activity A

ActivityA: onStart has been Called!

CurrentLocation: apiStart has been called by Activity A

ActivityA: onResume has been Called!

CurrentLocation: apiStart has been called by Activity A

Activity A: onMapReady has been called

CurrentLocation: onConnected has been called by Activity A

CurrentLocation: onLocationChanged has been called by Activity A

The user clicks on button then:

ActivityA: onPause has been Called!

ActivityB: onCreate has been Called!

CurrentLocation: CurrentLocation has been called by Acvitivy B

CurrentLocation: apiBuild has been called Activity B

ActivityB: setupLayout has been called!

ActivityB: onStart has been Called!

CurrentLocation: apiStart has been called by Activity B

ActivityB: onResume has been Called!

CurrentLocation: apiStart has been called by ActivityB

ActivityB: surfaceCreated has been called!

ActivityB: search has been called

CurrentLocation: getCurrentLocation has been called by ActivityB

Then it crashes! Due to getCurrentLocation returning null

Here is the stack trace when it crashes:

enter image description here

0

There are 0 best solutions below