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