How to avoid the ANR dialog Application Not Responding

后端 未结 3 1438
我寻月下人不归
我寻月下人不归 2021-01-03 13:38

I\'m working on an Android app, which uses an online service, which I need to load my Fragments. Basically, I have a menu, and each button replaces a Fragment underneath. Wh

3条回答
  •  轮回少年
    2021-01-03 13:48

    Is there a way to prevent that from happening?

    Yes: don't take so long on the main application thread. Use StrictMode to identify likely culprits (disk I/O, network I/O), and use Traceview to find out what else you are doing that might be taking too long.

    My app will always take a few seconds to load, because of the web service.

    That should be done in a background thread.

    I read on Google, that i could use AsyncTask, and finish the loading on the DoOnBackground method.. I don't know if that works, but I can't even try, because it won't let me change the view there, it throws an error about only the original thread can change the views

    Which is why you update the views from the onPostExecute() method, as is described in the documentation for AsyncTask.

提交回复
热议问题