/** * For whatever reason, wrapping a ProgressDialog in a Fragment prevents the "BadTokenException" * exception from happening in ActivityUnitTestCase classes. */ public class ProgressDialogFragment extends DialogFragment { public static ProgressDialogFragment newInstance(String text) { ProgressDialogFragment frag = new ProgressDialogFragment(); Bundle args = new Bundle(); args.putString("text", text); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { ProgressDialog dialog = new ProgressDialog(getActivity()); String text = getArguments().getString("text"); dialog.setMessage(text); return dialog; } }