Open mrdavidrees opened 5 years ago
+, need that
+1
+1
any updates ?
this would be awesome also because it won't work the normal scan, only streamscan works
+1
java.lang.ClassCastException: com.myapp.MainActivity cannot be cast to io.flutter.app.FlutterActivity
+1
+1
Hi guys, I was facing the same problem but I managed using Method Channel to emit an event to my MainActivity (responsible for running Flutter) that executes an Intent with the flag: FLAG_ACTIVITY_CLEAR_TOP, it's a flag that cleans every activity above the MainActivity, aka Base Activity.
Method Channel (Flutter):
Future<void> finishBarcodeReaderActivity() async {
const platform = MethodChannel('com.example/channel');
try {
await platform.invokeMethod('finishBarcodeReaderActivity');
} on PlatformException catch (e) {
log('ERROR: exitRunningActivity => Platform Error', error: e);
} catch (e) {
log('ERROR: exitRunningActivity => Unknown Error', error: e);
}
}
Stream (Flutter):
void startBarcodeScanStream() {
late Stream<dynamic>? barcodeStream;
late StreamSubscription? barcodeSubscription;
barcodeStream = FlutterBarcodeScanner.getBarcodeStreamReceiver(
'#ff6666',
'Fechar Leitor',
false,
ScanMode.DEFAULT,
);
barcodeSubscription = barcodeStream!.listen(
(value) async {
try {
Your logic ...
} catch (e) {
barcodeSubscription!.cancel();
finishBarcodeReaderActivity();
}
},
);
}
MainActivity (Kotlin):
package com.example
import android.content.Context
import android.content.Intent
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
class MainActivity : FlutterActivity() {
fun finishSpecificRunningActivity(context: Context) {
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
}
context.startActivity(intent)
}
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine);
MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"com.example/channel"
).setMethodCallHandler { call, result ->
if (call.method == "finishBarcodeReaderActivity") {
finishSpecificRunningActivity(this)
result.success(null)
} else {
result.notImplemented()
}
}
}
}
OBS: My MainActivity is in Kotlin, but yours could be in Java, you just need to translate.
Is your feature request related to a problem? Please describe. It's not possible to close the barcode scanner once you are happy with the result from the screen.
Describe the solution you'd like A method that will tell the barcode scanner to close
Describe alternatives you've considered FlutterBarcodeScanner.getBarcodeStreamReceiver("#000000", "Cancel", true).listen((d) => print(d)); await Future.delayed(Duration(seconds: 1)); Navigator.pop(context);
Additional context When I call navigator.pop it closed the screen behind the barcode scanner. It would be great if I could call FlutterBarcodeScanner.closeBarcodeScanner() once i have processed an item from the screen and verified it.
It happened with me too, but that happen because you're working with different activities, if you check the ActivityManager and check your running tasks in the MainAcitvity, it's possible to see two activities yours and the Barcode Scanner. So when you called the Navigator.pop(context) you're working under the MainActivity and not on the Barcode Scanner.
any updates??
any updates??
Is your feature request related to a problem? Please describe. It's not possible to close the barcode scanner once you are happy with the result from the screen.
Describe the solution you'd like A method that will tell the barcode scanner to close
Describe alternatives you've considered FlutterBarcodeScanner.getBarcodeStreamReceiver("#000000", "Cancel", true).listen((d) => print(d)); await Future.delayed(Duration(seconds: 1)); Navigator.pop(context);
Additional context When I call navigator.pop it closed the screen behind the barcode scanner. It would be great if I could call FlutterBarcodeScanner.closeBarcodeScanner() once i have processed an item from the screen and verified it.