BlockingDialog

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

License

License

GroupId

GroupId

com.jaredrummler
ArtifactId

ArtifactId

blocking-dialog
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

BlockingDialog
Show a dialog from a background thread and wait for a result.
Project URL

Project URL

https://github.com/jaredrummler/BlockingDialog
Source Code Management

Source Code Management

https://github.com/jaredrummler/BlockingDialog

Download blocking-dialog

How to add to project

<!-- https://jarcasting.com/artifacts/com.jaredrummler/blocking-dialog/ -->
<dependency>
    <groupId>com.jaredrummler</groupId>
    <artifactId>blocking-dialog</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.jaredrummler/blocking-dialog/
implementation 'com.jaredrummler:blocking-dialog:1.0.0'
// https://jarcasting.com/artifacts/com.jaredrummler/blocking-dialog/
implementation ("com.jaredrummler:blocking-dialog:1.0.0")
'com.jaredrummler:blocking-dialog:jar:1.0.0'
<dependency org="com.jaredrummler" name="blocking-dialog" rev="1.0.0">
  <artifact name="blocking-dialog" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.jaredrummler', module='blocking-dialog', version='1.0.0')
)
libraryDependencies += "com.jaredrummler" % "blocking-dialog" % "1.0.0"
[com.jaredrummler/blocking-dialog "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.android.support » support-annotations jar 25.3.1

Project Modules

There are no modules declared in this project.

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.

Versions

Version
1.0.0