Moritz
Moritz

Reputation: 10352

Fused location provider on Android Wear causes crash when restarting activity

I am experiencing a crash when starting, closing and restarting an activity on Android Wear that uses the fused location provider.

The scenario is: Starting activity that connects to the fused location provider, closing the activity (while disconnecting from the fused location provider) and restarting the app causes the newly launched activity to crash with the following exception:

01-31 18:51:15.319    2281-2281/com.example.test E/ActivityThread﹕ Performing pause of activity that is not resumed: {com.example.test/com.example.test.WearActivity}
java.lang.RuntimeException: Performing pause of activity that is not resumed: {com.example.test/com.example.test.WearActivity}
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3196)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3184)
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3159)
        at android.app.ActivityThread.access$1000(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

The crash can be experienced when the Android Wear device is connected to the phone and the fused location provider returns "fused" locations. When using the location provider without the connected phone the crash does not appear.

When relaunching the activity from the list of apps, the newly created activity goes through the create -> start -> resume phase and than directly into stop without pause.

This causes the watch to lockup so that you can not get into the voice command anymore. You can still scroll through cards on the home screen though. Pressing the device button to turn the screen on and off "unlocks" the device and the logcat prints the mentioned exception.

Sometimes it needs two or more restarts of the app to cause the crash but it is reproducible. Running the same app on a nexus 6 does not cause any problems.

The following code reproduces the issue:

public class WearActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {

private GoogleApiClient apiClient;
private LocationListener locationListener;

@Override
protected void onStart() {
    super.onStart();
    Log.v("Wear", "onStart");
    apiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    apiClient.connect();

}

@Override
protected void onStop() {
    super.onStop();
    Log.v("Wear", "onStop");
    if (locationListener != null) {
        FusedLocationApi.removeLocationUpdates(apiClient, locationListener);
        System.out.println("removing");
        locationListener = null;
    }
    if (apiClient.isConnected()) {
        apiClient.disconnect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    System.out.println("connection established");
    if (locationListener == null) {
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                System.out.println(location);
            }
        };
        FusedLocationApi.requestLocationUpdates(apiClient, createLocationRequest(), locationListener);
    }
}

private LocationRequest createLocationRequest() {
    return new LocationRequest()
            .setInterval(2000)
            .setFastestInterval(2000)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onConnectionSuspended(int i) {
    System.out.println("connection suspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    System.out.println("connection failed");
}
}

Seen on a Sony Smartwatch 3 running Android 5.0.1 (Build LWX48P).

Upvotes: 1

Views: 627

Answers (1)

Craig J
Craig J

Reputation: 177

Not sure if this is the problem but there is no need to be creating a new Google Api client in onStart each time. Instead it should be in onCreate, then in onStart just have apiClient.connect();

Upvotes: 0

Related Questions