アクティビティが始まる前に、なぜOnActivityResultがトリガーされるのですか?
-
29-09-2019 - |
質問
コードを継続する前に、バーコードをスキャンする必要があるアプリがあります。このコードを使用してスキャンアクティビティを開始します。
finish = (Button) findViewById(R.id.finishButton);
finish.setOnClickListener(new OnClickListener() {
public void onClick(View viewParam) {
/*Prompt the user to scan the barcode */
new AlertDialog.Builder(Visit.this)
.setMessage("Please Scan the clients barcode to complete the visit")
.setPositiveButton("Scan Barcode", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Start the scan application
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, 0);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Execute some method call
Toast.makeText(Visit.this, "Scan declined...", Toast.LENGTH_SHORT).show();
}
})
.show();
/* End of Scan prompt */
}
});
上記のコードは、ラベル付きのボタンにリスナーを設定します finished
. 。ボタンがクリックされると、ユーザーにバーコードのスキャンまたはキャンセルを求めるプロンプトが表示されます。
をクリックします バーコードをスキャンします ボタンは、スキャンを開始する新しいアクティビティを開始します。
スキャンからの返されたスキャンの結果を読むために次のコードを設定しました。
/* Return from scanning barcode */
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
;
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
Bundle extras = data.getExtras();
String result = extras.getString("SCAN_RESULT");
}
Toast.makeText(Visit.this, "request code: "+requestCode+" result code = "+resultCode+ "\nRESULT_OK: "+Activity.RESULT_OK, Toast.LENGTH_SHORT).show();
}
これは非常に単純に(今のところ)アクティビティがトーストメッセージに出力します。
私が抱えている問題は、です onActivityResult
スキャンバーコードボタンを押すとすぐにメソッドがトリガーされます。
スキャンプロセスは、ログキャットでスキャンの結果を見ることができるため、正常に機能します。ただし、すぐにトリガーされているため onActivityResult
メソッドはスキャン結果を取得することはなく、結果コードは常に-1です
ここで一歩踏み出しましたか?到達するためのやり方はありますか onActivityResult
アクティビティが実際に終了するまで待つには?
解決
私が使用しているバーコードスキャナーには、マニフェストの単一インスタンスを使用したタブインターフェイスに特定のバグがあるようです。
単一のインスタンス規定を削除することで、完全に機能するハックを行うことができました。
所属していません StackOverflow