Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlutterDriver: Use Android back button

I have a mobile application for Android with multiple pages and currently try to write simple integration tests for it...

The issue is that my app only uses internal Android back gestures, I have no back button or something like this.

Is it somehow possible to tell the FlutterDriver to go one page back? (Simulate a android internal back button?) Something like this:

driver.goPageBack()

Thank you so much. :)

like image 339
Machiavelli Avatar asked Jan 26 '26 19:01

Machiavelli


2 Answers

Try this in your Activity code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.YOUR_ACTIVITY);
    assert getSupportActionBar() != null;   //null check
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);   //show back button
}

@Override
public boolean onSupportNavigateUp() {
    finish();
    return true;
}
like image 98
tefek Avatar answered Jan 29 '26 09:01

tefek


In the Flutter repo there is a test that simulates the hardware back button by sending a message over the platform channel like this:

final ByteData message = const JSONMethodCodec().encodeMethodCall(const MethodCall('popRoute'));
await ServicesBinding.instance!.defaultBinaryMessenger.handlePlatformMessage('flutter/navigation', message, (_) { });

https://github.com/flutter/flutter/blob/6688e63f68ebba0919a5fe3c8f8432bd8a65f81b/packages/flutter/test/widgets/router_test.dart#L725-L726

like image 23
spkersten Avatar answered Jan 29 '26 09:01

spkersten



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!