Skip to content

jaredrummler/BlockingDialog

Repository files navigation

Blocking Dialog

Show a dialog from a background thread and wait for a result. Discussed on StackOverflow here and here.

API License Maven Central

Usage

1. Create a dialog that extends BlockingDialogFragment:

public class YesOrNoDialog extends BlockingDialogFragment<String> {

  @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity())
        .setMessage("Do you like Taylor Swift?")
        .setPositiveButton("YES", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            setResult("YES", false);
          }
        })
        .setNegativeButton("NO", new DialogInterface.OnClickListener() {
          @Override public void onClick(DialogInterface dialog, int which) {
            setResult("NO", false);
          }
        })
        .create();
  }
}

2. Show the dialog from a worker thread using BlockingDialogManager#showAndWait:

public class MyTask extends AsyncTask<Void, Void, Void> {

  WeakReference<Activity> weakActivity;

  ...

  @Override protected Void doInBackground(Void... params) {

    ...

    // Need input from the  user.
    // Show the dialog from the UI thread and wait on this background thread for a result
    Activity activity = weakActivity.get();
    String input = BlockingDialogManager.getInstance().showAndWait(activity, new YesOrNoDialog());

    if (input == null) {
      // user cancelled
    } else if (input.equals("YES")) {
      // do something
    }

    return null;
  }

}

See the demo project.

Download

Download the latest JAR or grab via Gradle:

compile 'com.jaredrummler:blocking-dialog:1.0.0'

License

Copyright 2017 Jared Rummler

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Show a dialog from a background thread and wait for a result.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages