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?