📜 ⬆️ ⬇️

Waiting for user reaction to Dialog in Android

Before inventing something, I, of course, googled (the author of this post already laid out his decision).
The fact is that in the UI stream you cannot wait, and the Activity provides an event model for working with dialogs: a common handler with a dialog identifier is called to close the dialog. This approach seemed to me not very convenient and I decided to do everything in my own way.
If you can't keep waiting for the UI stream, we will create another thread and wait for it, and if so, then I will need a thread synchronization mechanism. For this purpose, I wrote an elementary mutex. The lock () method does not return until unlock () is called.
public class Mutex { public synchronized void lock() throws InterruptedException { this.wait(); } public synchronized void unlock() { this.notify(); } } 

Next, I created a class for working with the dialog and included a mutex in it. The class initializes the dialog and stores the user's reaction after completing the dialog. I omitted the implementation of some class methods to focus only on an important part of the implementation.
All buttons are assigned handlers (1), which store the return value in a variable, and then close the dialog. The dialog handler (2) frees the mutex. Interface function (3) is waiting for the mutex to be released.
 public class SyncDialog { private Dialog mDialog; private Mutex mMutex; private int mResult; private Button mYesButton; private Button mNoButton; private Button mCancelButton; public SyncDialog(Context context) { mMutex = new Mutex(); mDialog = new Dialog(context); mDialog.setContentView(R.layout.dialog); findViews(); mYesButton.setOnClickListener(new OnClickListener() { // (1) public void onClick(View v) { mResult = YES_RESULT; mDialog.dismiss(); } }); mNoButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { mResult = NO_RESULT; mDialog.dismiss(); } }); mCancelButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { mResult = CANCEL_RESULT; mDialog.cancel(); } }); mDialog.setOnDismissListener(new OnDismissListener() { // (2) public void onDismiss(DialogInterface dialog) { mMutex.unlock(); } }); } public void waitForResponse() throws InterruptedException { // (3) mMutex.lock(); } } 

Well, actually, what I originally wanted: a method that waits for a user to respond (2). It should work in a separate thread.
First, we create a SyncDialog in the UI stream (1), then, in the background thread, we send the initialization and dialog start (3) to the UI thread, wait (4) and process the result (5).
 public class MyActivity extends Activity { private SyncDialog syncDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); syncDialog = new SyncDialog(this); // (1) } public void processDialogs(String... questions) { // (2) for (final String question : questions) { runOnUiThread(new Runnable() { // (3) public void run() { syncDialog.setText(question); syncDialog.show(); } }); int response; try { syncDialog.waitForResponse(); // (4) response = syncDialog.getResult(); } catch (InterruptedException e) { response = SyncDialog.CANCEL_RESULT; } if (response == SyncDialog.YES_RESULT) { // (5) Log.i(question, "Yes"); }else if (response == SyncDialog.NO_RESULT) { Log.i(question, "No"); }else break; } } } 

You can run processDialogs () using AsyncTask
 public class DialogsTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... params) { processDialogs(params); return null; } } 

To consistently display dialog boxes using an event-based approach, I had only the idea to create a list of structures that store data to display a dialog, and, upon closing the dialog, initialize a new dialog with the next structure in the list. This method seemed to me quite uncomfortable. I hope the article will be useful to those who are looking for a solution to this problem.

Thanks for attention.

The full source code of the example can be found here .

')

Source: https://habr.com/ru/post/131716/


All Articles