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

后端 未结 3 1788
一整个雨季
一整个雨季 2020-12-21 04:37

I\'m writing an activity in Android where the user modifies an SQL Database. The UI consists of an EditText where the user enters the name, and a Seekbar where the user ente

相关标签:
3条回答
  • 2020-12-21 05:17

    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?

    0 讨论(0)
  • 2020-12-21 05:25

    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.

    0 讨论(0)
  • 2020-12-21 05:27

    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.

    0 讨论(0)
提交回复
热议问题