私はBluetoothで常にデータを受信し、MainActivity
からMainActivity2
からIntent
にデータをプッシュします。 MainActivity
には、に切り替えることができるImageButton
があります。私は、変数ImageButton
をクリックしたときにtrue
でClicked
とonBackpressed()
false
を使用していますアクティビティの終了時にバックプレスまたは終了()が機能しません
。 ImageButton
をクリックすると、Bluetooth受信を開始するためにtrue/false
の条件が使用されます。 MainActivity2
でBluetoothデータを受信できますが、onBackpressed
はMainActivity2
を返して、MainActivity
に戻ります。その後、新しいデータが到着するとすぐに自動的にMainActivity2
が開始されます。 MainActivity2
は、ImageButton
とonBackPressed
をクリックしたときにのみデータを取得します。MainActivity2
は完全に閉じられていますが、それは起こりません。
MainActivity - ImgButtonFunction
public void onImgButtClick(View view){
Intent getNameScreenIntent = new Intent(MainActivity.this, MainActivity2.class);
final int result=1;
Clicked=true;
startActivityForResult(getNameScreenIntent,result);
}
MainActivity - データの小さな部分が受信ハンドラ
if (action.equals(UartService.ACTION_DATA_AVAILABLE)) {
final byte[] txValue = intent.getByteArrayExtra(UartService.EXTRA_DATA);
runOnUiThread(new Runnable() {
public void run() {
try {
String text = new String(txValue, "UTF-8");
/*Character[] Chararray = new Character[text.length()];
for (int i = 0; i < text.length(); i++) {
Chararray[i] = new Character(text.charAt(i));
}*/
/* Character[] Chararray = new Character[text.length()];
byte txByte=0;
for (int i = 0; i < txValue.length(); i++) {
Chararray[i] = new Character(text.charAt(i));}
*/
String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
/*for (String retval: text.split("\n")) {
Chararray[i]=retval;
listAdapter.add("[" + currentDateTimeString + "] RecvX: " + retval + "km/h");
}*/
String str1 = String.valueOf(txValue[0]);
listAdapter.add("[" + currentDateTimeString + "] RecvX: " +str1 + "km/h");
messageListView.smoothScrollToPosition(listAdapter.getCount() - 1);
if (Clicked==true) {
Intent ii = new Intent(MainActivity.this, MainActivity2.class);
ii.putExtra("RxString", str1);
startActivity(ii);
}
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
});
}
MainActivity2 - onBackPressedハンドラ
@Override
public void onBackPressed() {
Intent i=new Intent(this, MainActivity.class);
i.putExtra("Clicked",false);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
finish();
}
になり得ることができます第3のアクティビティからの結果を受け取るonActivityResult()です。 – GoGam
別個のリクエストIDを持つアクティビティを開始している間は、アクティビティが結果を返すアクティビティの数は関係ありません。 onActivityResultのこの要求IDを使用して、着信結果を除外します。 Googleに関連するいくつかの例があります。 – AndroidDevUser