user3290180
user3290180

Reputation: 4410

Android Wear: How does it work an activity life cycle?

I made a wearable app with activity.

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i("WEAR", "CREATE");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
        }
    });
    SensorManager  sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    Sensor hrs = sm.getDefaultSensor(Sensor.TYPE_HEART_RATE);
    sm.registerListener(hrListener, hrs, 3);

    if (savedInstanceState != null) {
        Log.i("WEAR", "RESTORE");
        // ... get previous sensor data from the bundle

    }
}

@Override
protected void onStop() {
    Log.i("WEAR", "STOP");
    super.onStop();
    sm.unregisterListener(hrListener, hrs);
}

@Override
protected void onDestroy() {
    Log.i("WEAR", "DESTROY");
    super.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    Log.i("WEAR", "SAVE");
    // ... save sensor data in the bundle
    super.onSaveInstanceState(savedInstanceState);
}

private SensorEventListener hrListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(final SensorEvent event) {
        final float hearRate = event.values[0];
        Log.i("SENSOR", hearRate);
        mTextView.setText(Float.toString(hearRate));
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
};

When it starts I see the flow of data from the sensor. After a while the screen return on the clock watchface and the Log says STOP and SAVE on my app debug flow. I relaunch the app but the bundle is null and I lost all my data saved in the bundle. onDestroy is never called so why I don't get any bundle? It's a Gear Live.

Upvotes: 3

Views: 2072

Answers (2)

krishna5688
krishna5688

Reputation: 1180

You need to add finish the app by swiping left to right or by long press (Need to code for the same). Once finish is called, onDestroy will get called.

Upvotes: 1

Syi
Syi

Reputation: 108

The action to close the wear app should be slide from left to right to close it. You will see onDestroy is called. If you just press the button ( I only have a moto 360 1st gen. i.e. the side button), it is like just put the app off the foreground. So just onStop and onStart pair is called. Hope this late answer will still help you.

Upvotes: 2

Related Questions