Android: Wait() the main thread while a dialog gets input in a separate Thread

丶灬走出姿态 提交于 2019-11-29 14:32:40

No, no, no! Do not block the UI thread. The system will raise an "Application Not Responding" error. Also, do not try to interact with the user from a non-UI thread.

When the user clicks "edit", don't start the edit method. Just pop up a dialog to collect the required information. Add a DialogInterface.OnClickListener to the positive button and (with the required information now in hand) start the edit method from there.

See the guide topic Dialogs for more information.

before invoke wait need get the object lock like this:

 synchronized(Thread.currentThread()) { //added
 try {
            Thread.currentThread().wait();
        } catch (InterruptedException e) {
            Log.e("Attractivometer","Main Thread interrupted while waiting");
            e.printStackTrace();
        }
 }

but why you want to make main thread wait?

First of all, you should not pause the main Thread, because everything will be frozen if you do that, and from an user perspective, that is not good at all.

And second, you should pop up a dialog with 2 buttons, (Done, Cancel) and allow user to go further by pressing one of those buttons. Here you can find out how to display a custom dialog: http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application

First rule of multi-thread programming in Android is that, You should never stop UI thread, and all the long running operations should be made in separate thread. And by long running operations I mean, SQLite database writing/ reading etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!