Blocking UI interaction on a Fragment

1.3k Views Asked by At

I want to be able to block all UI interaction with a fragment until a callback occurs.

I have two buttons: ButtonA and ButtonB.

ButtonA shows a progress bar and kicks-off an asynchronous thread that calls-back to the fragment when it's done. In the meantime, someone can press ButtonB which I don't want to allow.

My solution was to spin up another fragment which is transparent and intercepts all clicks. However there appears to be delay between FragmentManagers commit() and the fragment actually working.
I've tried calling executePendingTransactions() but I still end up with threading issues whereby the fragment isn't in a state to accept onClick events before the user hits ButtonB.

Is there a more elegant solution?

Thanks,

John

5

There are 5 best solutions below

4
Semyon Danilov On

Call buttonB.setEnabled(false); after clicking buttonA.

CustomButton extends View {

     private boolean mIsEnabled = true;

     public void setEnabled (boolean enabled) {
        this.mIsEnabled = enabled;
     }


     @Override
     public void onClick() {
          if (mIsEnabled) {
               mOnClickListener.onClick();
          } else {
              return;
          }
     }


}
0
Snicolas On

Another option is to use a progress dialog fragment and set it to be non cancelable. It will cover the fragment and prevent the underlying fragment from receiving any touch event.

1
Aparna Suresh On

Instead of calling another fragment,u can have another tranparent view with a progress dialog above the present view and make its visibility VIEW or GONE accordingly.Else u can simply show a prgress dialog with cancelable parameter as false.

0
SHASHIDHAR MANCHUKONDA On

I didnt understood the question perfectly.. hope it may helps you. when you adding transaprent fragment over it make the transparent layout clickable=true

if a view is mentioned as clickable it does not pass touch events to below views.

sorry if i understtod your question wrong.

0
Snicolas On

Can't button A put its containing activity in a given state (with a boolean flag raised in its listener) and button B read that flag before doing any stuff ?

I think it's not only a UI issue here but also some presentation logic and mini-state machine that you should implement. This mechanism plus the fragment you already have should be enough to prevent gaps in the sequence of executions of UI Thread events.