Search code examples
androidbluetoothrxandroidble

How to pause execution of back button until subscription completes?


I'm running into tons of problems using RxAndroidBle when events like back button press happen and I was wondering what the best way to pause execution of the back button is. I basically want to create a lock until a particular bluetooth operation completes. Once the operation completes I want to finish the execution of the back button. Anyone have any ideas?


Solution

  • Just override the back button and use some flag? E.g.

    private boolean delayedBack = false; // as member field
    
    ...other Activity code
    
    @Override
    public void onBackPressed(){
       if(someCondition)
          super.onBackPressed();
        else
          delayedBack = true;  
    }
    

    Then when some asynchronous operation finishes just check that flag:

     if(delayedBack){
       super.onBackPressed();
       delayedBack = false; // in case you don't want to finish the Activity
      }
    

    Of course instead of using super.onBackPressed(); which in most cases will finish the Activity you can perform any other action you'd perform when the user hits the back button.