processInfo()というメソッドを呼び出すときに問題が発生します。この方法では、基本的にNFCタグまたはQRコードから収集した情報を処理し、DBに書き込みます。特定の状況に応じて、processInfoメソッドは情報がDBに書き込まれる方法を処理します。processInfoメソッドはブール値を返します。これをtrueに設定すると、DB内の情報がWebサーバーに送信されます。
processInfo内に、条件Aの場合はDBに書き込み、trueを返すというロジックがあります。これにより、wewbserviceに送信されます。条件Bの場合は、Alertdialogボックスを表示します。Alertdialogボックスには、[OK]ボタンと[キャンセル]ボタンがあります。okが押された場合は、条件Aで何が起こるかを実行し、CANCELが押された場合は、ダイアログを閉じてfalseを返します。
何が起こっているのかというと、条件Bが発生した場合、ダイアログは期待どおりに表示されますが、いずれかのボタンが押される前に呼び出し元のメソッドに戻ります。少なくとも1つのボタンが押されるまでアプリをハングさせるにはどうすればよいですか?
を使ってみましたwhile(! alertDialog.isShowing == true) -> return boolean
。ただし、メソッドの呼び出し後にドロップアウトしalertDialog.show()
て戻ります。
success = false;
if(condition A) {
// write to DB and return to caller
return success = true;
} else {
success = false;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(NfcscannerActivity.this);
// set title
alertDialogBuilder.setTitle("Please logout after ");
// set dialog message
alertDialogBuilder.setMessage("Click Ok to return to the menu")
.setCancelable(false)
.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
success = true;
// write to DB
}
})
.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
success = false;
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
return success;
}