1

How to disable all touch event when a process is running on Fragments. Our device has a built-in printer in it. while printing and rapidly tapping the screen it makes the app crash, How can I assure that the screen is untouchable?. I have tried to use

getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

and

getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

and the crash still occurs. I also try to add an ID on my parent LinearLayout and try to enable and disable it. but still, nothing happens?. What is the alternative code for it? Thanks!

Error Logs

02-26 18:16:37.000 18941-18946/com.jti.mikee.jti_pos I/dalvikvm: threadid=3: reacting to signal 3 02-26 18:16:37.060 18941-18946/com.jti.mikee.jti_pos I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt' 02-26 18:16:37.110 18941-18941/com.jti.mikee.jti_pos I/HAL_JNI: - native_printer_close(), result = 0 02-26 18:16:37.130 18941-18941/com.jti.mikee.jti_pos I/Choreographer: Skipped 357 frames! The application may be doing too much work on its main thread.02-26 18:16:40.740 18941-18941/com.jti.mikee.jti_pos A/libc: Fatal signal 6 (SIGABRT) at 0x00000419 (code=0), thread 18941 (i.mikee.jti_pos)

XML Layout

https://ghostbin.com/paste/ob2fa

Rubick
  • 296
  • 3
  • 22
  • you can take one transparent `view` over your fragment. and while your background process is going on make, `setClickable` to true for that view and after completing make it false again. – V-rund Puro-hit Feb 26 '19 at 10:02
  • Do you get a stacktrace with your crash? IF yes, can you post it? I ask because disabling all touch events sounds like an extreme solution to any problem. – PPartisan Feb 26 '19 at 10:19
  • @PPartisan post updated – Rubick Feb 26 '19 at 10:21
  • Ok, so it's an ANR rather than a crash - can you extract that traces file? run `adb shell cat /data/anr/traces.txt` or `adb pull /data/anr/traces.txt {local_path_to_save_file}` and read the contents. Evidently something is blocking the UI thread. – PPartisan Feb 26 '19 at 10:25
  • I have tried to run it but it says device not found. sorry new on this retrieving traces.txt – Rubick Feb 26 '19 at 10:28
  • When you're connected to a device that's experienced this issue via adb, run `adb shell` then `cd /data/anr/` and see what's inside with `ls`. Get the filename (if there is one), `exit`, and then pull to your current directory with `adb pull /data/anr/{file_name}`. There is some more info [here](https://developer.android.com/topic/performance/vitals/anr) and [here](https://developer.android.com/training/articles/perf-anr) I don't think trying to prevent touch events will solve your problem - you need to ensure long running tasks are executed off the UI thread. – PPartisan Feb 26 '19 at 10:32
  • permission denied on `cd data`. I guess that's our device manufactuer security. – Rubick Feb 26 '19 at 10:35
  • @MikeGorospe Yes, you wont be able to on a `user` build, it needs to be a `userdebug` build, with debugging enabled. You could try running `adb root; adb remount` but that probably wont be enough. Best bet then is to try and profile your app and find out what's blocking the UI thread. The links I posted above can help with that. – PPartisan Feb 26 '19 at 10:37
  • i would like to suggest you to learn a little deep about touch events **onInterceptTouchEvent** vs **dispatchTouchEvent** [here](https://stackoverflow.com/questions/9586032/android-difference-between-onintercepttouchevent-and-dispatchtouchevent) – Storytellerr Feb 26 '19 at 10:05

2 Answers2

1

You can do by using a trick.

  • Show a transparent view above your root view to disable click.
  • Hide that view for enable clicks

Thank you

Sohel S9
  • 246
  • 1
  • 15
0

override dispatchTouchEvent in activity

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!isSomethingInProcess) {
            return super.dispatchTouchEvent(ev);
        } else {
            return false;
        }
    }
parliament
  • 371
  • 1
  • 5
  • 18
  • I use `Fragment` and the printing is on `Fragment` only – Rubick Feb 26 '19 at 09:57
  • define a variable in your activity like "boolean isSomethingInProcess".. when your process starts in fragment, try to set variable like this ((YourActivity)getActivity).isSomethingInProcess = true; it will handle the rest.. don't forget to put above method (dispatchTouchEvent) to your activity – parliament Feb 26 '19 at 10:00