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

14,715

Solution 1

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.

Solution 2

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.

Solution 3

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?

Share:
14,715

Related videos on Youtube

W.K.S
Author by

W.K.S

#SOreadytohelp

Updated on June 27, 2022

Comments

  • W.K.S
    W.K.S almost 2 years

    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 enters how attractive the person is. Underneath there are a bunch of buttons: add, edit, view, delete.

    When the user clicks on the "Edit" button, an input dialog is displayed asking the user to input the record number. Once that is done, that record is loaded.

    The problem I was having was that the inputdialog would be displayed and while the user entering the record no, the rest of the edit method would carry on so that by the time the user was done entering the input - nothing happened because the function had already been completed.

    In order to solve this problem, I decided to use Multi-Threads (which I do not have much experience using). When the edit button is pressed, the main UI Thread is blocked (using wait() - this is because I don't want the UI to be active while the user is entering the record id.) and the input dialog is displayed in a seperate thread.

    Once the input has been entered, the thread is notified and the rest of the edit function continues. (The code is below).

    The problem is that when I call the wait function on the UI Thread, I get an error that says "object not locked by thread before wait() ". How do I lock the UI Thread?

    I know in general one shouldn't block the UI Thread, but I think it's okay in this case because I don't want it to accept any user input.

    Thanks for your help.

    public class Attractivometer extends Activity implements OnClickListener {
    
        private Button      buttonAddRecord,    buttonEditRecord,   buttonSaveChanges;
        private Button      buttonDeleteRecord, buttonViewRecord;
        private EditText    fieldName;
        private SeekBar     seekbarAttractiveness;
        private String      inputFromInputDialog=null;
        private Thread      inputThread;
    
    
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.Attractivometer);
    
            buttonAddRecord         = (Button) findViewById(R.id.buttonAddRecord);
            buttonSaveChanges       = (Button) findViewById(R.id.buttonSaveChanges);
            buttonEditRecord        = (Button) findViewById(R.id.buttonEditRecord);
            buttonDeleteRecord      = (Button) findViewById(R.id.buttonDeleteRecord);
            buttonViewRecord        = (Button) findViewById(R.id.buttonViewRecord);
    
            fieldName               = (EditText) findViewById(R.id.fieldName);
    
            seekbarAttractiveness           = (SeekBar) findViewById(R.id.seekbarAttractiveness);
    
            buttonAddRecord.setOnClickListener(this);
            buttonSaveChanges.setOnClickListener(this);
            buttonEditRecord.setOnClickListener(this);
            buttonDeleteRecord.setOnClickListener(this);
            buttonViewRecord.setOnClickListener(this);
        }
    
    
        public void onClick(View clickedItem)
        {
    
            switch(clickedItem.getId())
            {
            case R.id.buttonAddRecord:
                //.....
                break;
    
    
            case R.id.buttonSaveChanges:
              //... 
                break;
    
            case R.id.buttonEditRecord:
    
                inputThread = new Thread(new Runnable(){
    
                    public void run()
                    {
                        showInputDialog("Enter Record ID", InputType.TYPE_CLASS_NUMBER);
                    }
    
                });
                inputThread.start();
    
    
                try {
                    Thread.currentThread().wait();
                } catch (InterruptedException e) {
                    Log.e("Attractivometer","Main Thread interrupted while waiting");
                    e.printStackTrace();
                }
    
                try {
                    inputThread.join();
                } catch (InterruptedException e) {
                    Log.e("Attractivometer","Input Thread interrupted while joining");
                    e.printStackTrace();
                }
    
                int recordId = Integer.parseInt(inputFromInputDialog);
                if(recordId!=null)
                {
                    AttractivometerSQLHandler AttractivometerDatabaseHandler = new AttractivometerSQLHandler(this);
                    AttractivometerDatabaseHandler.openDatabase();
                    String recordName = AttractivometerDatabaseHandler.getName(recordId);
                    String recordAttractiveness = AttractivometerDatabaseHandler.getAttractiveness(recordId);
    
                    if(recordName==null || recordAttractiveness==null )
                    {
                        //no record found.
                        Toast.makeText(this, "No record with that ID found", Toast.LENGTH_SHORT).show();
                    }else
                    {
                        fieldName.setText(recordName);
                        seekbarAttractiveness.setProgress( Integer.parseInt(recordAttractiveness) );
                        recordIsOpen(true);
                    }
    
    
                    AttractivometerDatabaseHandler.closeDatabase();
                }else
                    //No input.
                recordIsOpen(false);
    
                break;
    
            case R.id.buttonDeleteRecord:
                //...
                break;
    
            case R.id.buttonViewRecord:
                //....
    
            }
        }
    
        private void showInputDialog(String prompt, int inputType)
        {
            AlertDialog.Builder inputDialog = new AlertDialog.Builder(this);
    
            inputDialog.setTitle("Record No.");
    
            final EditText fieldInput = new EditText(this);
            fieldInput.setInputType(inputType);
            fieldInput.setHint(prompt);
    
            inputDialog.setView(fieldInput);
            inputDialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
            {
    
                public void onClick(DialogInterface arg0, int arg1)
                {
                    inputFromInputDialog = fieldInput.getText().toString();
                    inputThread.notify();
    
                }
            });
    
            inputDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
            {
    
                public void onClick(DialogInterface arg0, int arg1)
                {
                    inputFromInputDialog = null;
                    inputThread.notify();
                }
            });
    
            inputDialog.show(); 
        }   
    }