Android Wear: how does the activity lifecycle work?

I made a downloadable 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 data flow from the sensor. After a while, the screen will return to the timezone and the log says STOP and SAVE on the application debug thread. I restart the app, but the package is zero and I have lost all my data saved in the bundle. onDestroy

never called that why i don't receive the package? This is Gear Live.

+3


source to share


2 answers


The action to close the wear app needs to be slide

left to right to close it. You will see onDestroy

. If you just press a button (I only have a Moto 360 1st generation i.e. the side button) it looks like a simple app from the foreground. Therefore, only the pair onStop

and are onStart

called. Hope this late answer still helps you.



+2


source


You need to add end app by swiping left or right or long press (you need to provide a code for it). Once the call is complete, onDestroy will be called.



+1


source







All Articles