Ramakishna Balla
Ramakishna Balla

Reputation: 1020

Unable to Tick the Checkbox on the UI based on the Value Stored in & Retrieved from SharedPreferences

I'm using LightSensor and a Checkbox to enable or disable the LightSensor by the user. Used SharedPreferences to store and retrieve the CheckBox status but unable to make it work the way I want like:

  1. If the user clicks the LightSensor Checkbox and starts the operation and exits the application and when he comes back to that application, the LightSensor Checkbox should be 'Ticked' on the UI - This is not happening even though I've used onResume() method and implemented my code like below:

  2. If the user UNclicks the LightSensor Checkbox and starts the operation and exits the application and when he comes back to that application, in this case, LightSensor Checkbox unchecked on the UI. This is working fine.

Code snippets below for onPause(), onResume(). There are NO errors but it just NOT working the way I explained above.:

Latest Code looks like this

@Override
        public void onPause() {
            super.onPause();
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putBoolean("LightSensorValue", isLightSensorON);
            editor.commit();
            // on pause turn off the flash
            turnOffFlash();
           // tggle_Text_Button.setChecked(false);
            mSensorManager.unregisterListener(this);
        }


        @Override
        public void onResume() {
            super.onResume();

            // on resume turn on the flash
            if (hasFlash) {

                // turnONFlash();
                tggle_Text_Button.setChecked(false);
                // Setting the Default Value for the CheckBox to 'False' during
                // resume and retrieving the actual value
                // from the SharedPreferences
                if (mSharedPreferences.getBoolean("LightSensorValue", false)) {
                    ChkBox_LightSensor.setChecked(true); // to Tick The CheckBox
                                                            // on the UI if the
                                                            // SharedPreferences
                    // returns 'True' value
                    isLightSensorON = true;
                    turnONLightSensor();
                } else {
                    ChkBox_LightSensor.setChecked(false);
                    isLightSensorON = false;
                    turnOFFLightSensor();
                }
            }

        }

        @Override
        public void onStart() {
            super.onStart();

            // on starting the app get the camera params
            getCamera();
        }

        @Override
        public void onStop() {
            super.onStop();

            // on stop release the camera
            if (camera != null) {
                camera.release();
                camera = null;
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub

        }

        /*
         * 
         * When light sensor detects, it's value below <2, the flash light
         * automatically gets ON and in case if the light sensor detects, it's
         * values above 2...the flash light automatically gets OFF
         */
        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub

            float lux = event.values[0];
            int luxInt = (int) lux;
            // tV_light_Value.setText(String.valueOf(lux));
            Log.d("BRK0018 - LightSensor", "New Light value occurred : " + lux
                    + "   :   " + luxInt);
            if (event.values[0] <= 2.0) {
                turnONFlash();
                Log.d("BRK0018 - LightSensor",
                        "New Light value LOW occurred : " + luxInt);
                Log.d("BRK0018 - LightSensor",
                        "LightSensor switched on the Flash");
            } else {
                turnOffFlash();
                Log.d("BRK0018 - LightSensor", "New Light value is HIGH : "
                        + luxInt);
                Log.d("BRK0018 - LightSensor",
                        "LightSensor switched OFF the Flash");
            }

        }

        // When Light Sensor Checkbox is checked, below is the initialization of
        // the LightSensor and the
        // SensorManager

        @Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            boolean isLightSensorON = ChkBox_LightSensor.isChecked();
            if (isLightSensorON) {
                turnONLightSensor();
            } else {
                turnOFFLightSensor();
            }

        }

        private void turnONLightSensor() {
            // TODO Auto-generated method stub
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putBoolean("LightSensorValue", isLightSensorON);
            editor.commit();
            isLightSensorON = true;
            mSensorManager = (SensorManager) getActivity().getSystemService(
                    Context.SENSOR_SERVICE);
            if (mSensorManager.getSensorList(Sensor.TYPE_LIGHT).size() != 0) {
                Sensor mLightSensor = mSensorManager.getSensorList(
                        Sensor.TYPE_LIGHT).get(0);
                mSensorManager.registerListener(this, mLightSensor,
                        SensorManager.SENSOR_DELAY_NORMAL);
                Toast.makeText(getActivity(), "LightSensor is ACTIVE now", 1000)
                        .show();
            }
        }

        private void turnOFFLightSensor() {
            // TODO Auto-generated method stub
            isLightSensorON = false;
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putBoolean("LightSensorValue", isLightSensorON);
            editor.commit();

            Toast.makeText(getActivity(), "Light Sensor is INACTIVE now", 1000)
                    .show();
        }

    }

Upvotes: 0

Views: 64

Answers (2)

Ramakishna Balla
Ramakishna Balla

Reputation: 1020

**Latest Updated answer by me.**

Sharing the details such that it can be helpful to other newbies like me.

I'd tried the mSensorManager.unregisterListener(this);to have it in elsecondition of theonResume() and also in 'turnOFFLightSensor()' but in both cases, the app was crashing without any error notes.

Then again, looked in to the code and there found the bug :(

In onClick() of the LightSensor Checkbox., I've placed the mSensorManager.unregisterListener(this); apart from it placing in onPause() already. That resolved the issue.

**Bottomline, in simple terms ** when I Uncheck the box, immediately unRegistered the LightSensor., that made my day ! :)

@Override
        public void onClick(View view) {
            // TODO Auto-generated method stub
            boolean isLightSensorON = ChkBox_LightSensor.isChecked();
            if (isLightSensorON) {

                turnONLightSensor();
            } else {
                turnOFFLightSensor();
                mSensorManager.unregisterListener(this);
            }

        }

BTW, A very big thanks to @Eugen Martynov for helping me all the way !

Upvotes: 0

Eugen Martynov
Eugen Martynov

Reputation: 20140

From you code I don't see if you ever set isLightSensorON = true; from user input. As well every onPause you save false to preferences so on every onResume you will get it off.

Upvotes: 2

Related Questions