3

I have a button that is supposed to finish the entire application when being pressed. The button press opens a dialog, and after a password is inserted the dialog's positive listener supposed to close the application. This is the dialog's positive listener code:

.setPositiveButton("Done", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        boolean res = ApplicationDataManager.checkEntryDetails(userName.getText(), password.getText());
                        if(res){
                            logger.writeLog(LogType.INFO, TAG, "createDialog. Session ended.");
                            //message for ending session.
                            Toast.makeText(MenuActivity.this, "Session successfully ended!",
                                    Toast.LENGTH_SHORT).show();


                            // stop both sensors and video recording.
                            stopService(sensorIntent);
                            stopService(videoRecordIntent);
                            menuActivityReference.finishAffinity();
                            System.exit(0);

                        }else{
                            logger.writeLog(LogType.INFO, TAG, "createDialog. wrong username or password for End Session");
                            //message for wrong input
                            Toast.makeText(MenuActivity.this, "Wrong user name or password! Please try again.",
                                    Toast.LENGTH_SHORT).show();
                            //show dialog again for retry.
                            showDialog();
                        }

I have an unbound service, that is being stopped by stopService(sensorIntent);, and started like this:

// create and start the sensors and video recording services in the background.
        sensorIntent = new Intent(this, SensorsService.class);
        sensorIntent.putExtra(PATIENT_KEYWORD, patient);
        sensorIntent.putExtra(SENSORS_FILE_PATH, sensorFile);
        startService(sensorIntent);

but unfortunately, the service onDestroy() function is not being called.

    public void onDestroy() {
        logger.writeLog(LogType.INFO, TAG, "onDestroy. Unregistering sensors");
        // unregister all of the sensors from sensorManager listener.
        for (Sensor s : sensorList) {
            sensorManager.unregisterListener(this, s);
        }
        super.onDestroy();
    } 

To be clear I want to shut down the application when The positive button is pressed (and the password is correct).

I have a logger that is writing to a file when the method is called, so I know its not being called. Also tried to debug and didn't get there. After the call to stopService(sensorIntent); I'm closing the application by with the following lines:

menuActivityReference.finishAffinity();
System.exit(0);

while menuActivityReference is just a reference to the activity. (This is being done from a button listener so I can't use the this keyword).

After a wide search, I saw that onDestroy() is not always being called (at least not immediately) so my questions are:

  1. Is there anything wrong with this behavior? Should I finish my app differently?
  2. Is there a callback that is being called in android after System.exit()?
  3. If there isn't, is it a good practice to implement a function that will do the same action as my onDestroy() and to call it before stopService()?
Daniel
  • 1,895
  • 9
  • 20

0 Answers0