0

I have a situation here. On the StartActivity.java onCreate method I will check whether a user turn on their GPS or not. If the GPS is off, user need to turn on and go to settings. So the activity will be onPause. When the user enter back to the apps, it will go through the onResume.

Then it will move to another activity called MainActivity.java. In this activity will generate a maps.

StartActivity.java

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        if (Utils.isGPSEnabled(this)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(PERMISSIONS_LOCATION, REQUEST_LOCATION);
            } else {
                proceed();
            }
        } else {
            Utils.T(this, "Please enable your GPS location");
            Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(viewIntent);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        proceed();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode){
            case REQUEST_LOCATION :
                if (Utils.permissionGranted(requestCode, REQUEST_LOCATION, grantResults, 1)){
                    proceed();
                } else {
                    Utils.t(this, "Go to settings and enable permissions");
                }
        }
    }


    public void proceed() {
    new CountDownTimer(3000, 100) {
        public void onTick(long millisUntilFinished) {
            if (Math.round((float)millisUntilFinished / 1000.0f) != countTime) {
                countTime = Math.round((float) millisUntilFinished / 1000.0f);
            }
        }
        public void onFinish() {
            tv_detect.setText("Done!");
            intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
            finish();
        }
    }.start();
}

MainActivity.java

public class MainActivity extends AppCompatActivity implements
        OnMapReadyCallback, LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener, GoogleMap.OnMarkerClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buildGoogleApiClient();
    }

    protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
   }


   @Override
   protected void onPause() {
    super.onPause();
    // stop location updates when Activity is no longer active
    if (mGoogleApiClient != null) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
       }
   }

  // The rest of method is already implemented
}

Error :

Unable to pause activity {xx.xxx.com.xxx/xx.xxx.com.xxx.ui.activity.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.

Problem : Error happen when I put proceed() method on onResume and it will generate twice MainActivity.java twice. When I remove the proceed() from onResume it will works fine, but I need to use onResume because when the activity is paused (user go to settings to turn on the GPS) and coming back to the apps it will stuck and do nothing which is not convenient.

Any help is really appreciated. Thanks

Sariyanti
  • 37
  • 1
  • 5

2 Answers2

0

you need to check that mGoogleApiClient is connected in onPause() of MainActivity.java so you should do this in onPause()

if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
   }
}
Omer
  • 650
  • 5
  • 15
0

check this condition in onResume(), because when first time create StartActivity.java it call onResume() and when back from setting it call onResume second time.

if (Utils.isGPSEnabled(this)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(PERMISSIONS_LOCATION, REQUEST_LOCATION);
        } else {
            proceed();
        }
    } else {
        Utils.T(this, "Please enable your GPS location");
        Intent viewIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(viewIntent);
    }