@@ -582,12 +582,17 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
582
582
}
583
583
}
584
584
585
- // The app cannot currently handle the back button action to allow users
586
- // to move between screens back and forth. What happens is the app is "moved"
587
- // to background as if "home" button were pressed.
588
- // To avoid unexpected behaviour, we prompt users and force the app process
589
- // to exit which helps with preserving phone's resources by shutting down
590
- // all goroutines.
585
+ // Handle Android back button behavior:
586
+ //
587
+ // By default, if the webview can go back in browser history, we do that.
588
+ // If there is no more history, we prompt the user to quit the app. If
589
+ // confirmed, the app will be force quit.
590
+ //
591
+ // The default behavior can be modified by the frontend via the
592
+ // window.onBackButtonPressed() function. See the `useBackButton` React
593
+ // hook. It will be called first, and if it returns false, the default
594
+ // behavior is prevented, otherwise we proceed with the above default
595
+ // behavior.
591
596
//
592
597
// Without forced app process exit, some goroutines may remain active even after
593
598
// the app resumption at which point new copies of goroutines are spun up.
@@ -601,20 +606,37 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
601
606
// https://developer.android.com/guide/components/activities/tasks-and-back-stack
602
607
@ Override
603
608
public void onBackPressed () {
604
- new AlertDialog .Builder (MainActivity .this )
605
- .setTitle ("Close BitBoxApp" )
606
- .setMessage ("Do you really want to exit?" )
607
- .setPositiveButton (android .R .string .yes , new DialogInterface .OnClickListener () {
608
- public void onClick (DialogInterface dialog , int which ) {
609
- Util .quit (MainActivity .this );
610
- }
611
- })
612
- .setNegativeButton (android .R .string .no , new DialogInterface .OnClickListener () {
613
- public void onClick (DialogInterface dialog , int which ) {
614
- dialog .dismiss ();
615
- }
616
- })
617
- .setIcon (android .R .drawable .ic_dialog_alert )
618
- .show ();
609
+ runOnUiThread (new Runnable () {
610
+ final WebView vw = (WebView ) findViewById (R .id .vw );
611
+ @ Override
612
+ public void run () {
613
+ vw .evaluateJavascript ("window.onBackButtonPressed();" , value -> {
614
+ boolean doDefault = Boolean .parseBoolean (value );
615
+ if (doDefault ) {
616
+ // Default behavior: go back in history if we can, otherwise prompt user
617
+ // if they want to quit the app.
618
+ if (vw .canGoBack ()) {
619
+ vw .goBack ();
620
+ return ;
621
+ }
622
+ new AlertDialog .Builder (MainActivity .this )
623
+ .setTitle ("Close BitBoxApp" )
624
+ .setMessage ("Do you really want to exit?" )
625
+ .setPositiveButton (android .R .string .yes , new DialogInterface .OnClickListener () {
626
+ public void onClick (DialogInterface dialog , int which ) {
627
+ Util .quit (MainActivity .this );
628
+ }
629
+ })
630
+ .setNegativeButton (android .R .string .no , new DialogInterface .OnClickListener () {
631
+ public void onClick (DialogInterface dialog , int which ) {
632
+ dialog .dismiss ();
633
+ }
634
+ })
635
+ .setIcon (android .R .drawable .ic_dialog_alert )
636
+ .show ();
637
+ }
638
+ });
639
+ }
640
+ });
619
641
}
620
642
}
0 commit comments