1

I have a button in my app that starts a custom service class when clicked. Then, when clicked again, I stop the service and display a progress dialog.

The onClick of that buttons looks like this:

public void onClick(View v) {
  if (!recordingStarted){
    try{    
      recordingStarted = true;
      mainActivity.startService(new Intent(mainActivity, SensorService.class));
    } catch (SQLException e){
      mainActivity.logger.e(getActivity(),TAG, "SQL error insertSubject()", e);
    }
  } else {
    dialog = new ProgressDialog(mainActivity);
    dialog.setMessage("Stopping...");
    dialog.setTitle("Saving data");
    dialog.setProgressNumberFormat(null);
    dialog.setCancelable(false);
    dialog.setMax(100);
    dialog.show();

    mainActivity.stopService(new Intent(mainActivity, SensorService.class));
    startButton.setEnabled(false);
  }
}

Within the onDestroy method of my service, I'm checking a few things and if the checks pass, I use event bus to send a message back to the fragment so that it can dismiss the progress dialog:

public void onDestroy() {
  if (some condition is true){
    //Send message to dismiss progress dialog
    EventBus.getDefault().post(new DismissDialogEvent("dismiss"));

    //Close databases and stop other things
    unregisterListener();
    dbHelper.close();
  }
}

My DismissDialogEvent class is here:

public class DismissDialogEvent {
  public final String message;

  public DismissDialogEvent(String message) {
    this.message = message;
  }
}

And in my fragment I'm doing this:

@Override
public void onResume() {
  super.onResume();
  EventBus.getDefault().register(this);
}

@Override
public void onPause() {
  EventBus.getDefault().unregister(this);
  super.onPause();
}

@Subscribe
public void onEvent(DismissDialogEvent event){
  if (event.message.equals("dismiss")){
    dialog.dismiss();
  }
}

So what should be happening, is when the button is clicked the second time, a progress dialog will display, the service will check some things and then send a dismiss message back to the fragment. The fragment will receive the message, and then dismiss the progress dialog

The problem is that the progress dialog seems to get dismissed instantly (as soon as its created), so it doesn't show at all. It's almost like its not waiting for the dismiss event before proceeding to dismiss the dialog. I can confirm that the dialog does show by commenting out the dialog.dismiss() line

Where am I going wrong with my code to cause this?

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Simon
  • 9,762
  • 15
  • 62
  • 119
  • What is the rest of your service doing? Have you verified that its onDestroy method is being called at the time you expect? – Doug Stevenson Mar 31 '16 at 05:47
  • The service just polls sensors, stores them every 10ms to the database using an executor service, acquires a wake lock so the sensor reading can still happen regardless of screen state etc. And yes, `onDestroy` gets called correctly - I can see that by just logging something to logcat – Simon Mar 31 '16 at 05:54

1 Answers1

0

When you start service at that time first register event bus and than start service and after dialog dismiss please unregister your eventbus will help you

J.D.
  • 1,401
  • 1
  • 12
  • 21
  • So if I register eventbus in my `onClick` immediately before starting the service, and then unregister is immediately after stopping the service, I get a message saying `No subscribers registered for event class com.example.app.DismissDialogEvent` – Simon Mar 31 '16 at 06:01
  • @Simon unregister after dialog dismiss – J.D. Mar 31 '16 at 06:03
  • I have the same problem if I do that - the dialog doesn't show and dismisses instantly – Simon Mar 31 '16 at 06:07
  • @Simon Please put Log in onDestroy of your service and check whether your service finish work faster second time and call destroy after work finish. – J.D. Mar 31 '16 at 06:10
  • @Simon Please check whether your service start second time and stop immediately at this point your onDestroy is called and event fire – J.D. Mar 31 '16 at 06:13
  • So when I start my service I am 100% sure it is not stopping immediately as it completes all of the other tasks it needs to do. If, for example, I add `SystemClock.sleep(2000)` at the very beginning of my `onDestroy`, I would expect the button to be clicked, a dialog to show, the phone to lock for 2 seconds, and then dismiss. But I don't see the dialog for 2 seconds... – Simon Mar 31 '16 at 06:22
  • @Simon Make it 10 to 20 second wait and check whether it is working or not. or may sleep work on other thread and service call onDestroy immediately – J.D. Mar 31 '16 at 06:25
  • 20 seconds doesnt seem to work either. Is there any other way I can test it without sleep? – Simon Mar 31 '16 at 06:46