Skip to content

Commit

Permalink
frontend/android: check if webview can go back or close the app
Browse files Browse the repository at this point in the history
This should replace native Android back behavior with either going
back in the webview or closing the app.

This is using the native goBack method but it seems that the app
only goes back to account summary. This could probably be improved
later and seems already useful with just going back to summary.

On the account summary it cannot go back any further so it will
trigger the old do-you-want-to-quit alert.

Replaced deprecated onBackPressed override.
  • Loading branch information
thisconnect committed May 8, 2024
1 parent 365dfd4 commit 772821e
Showing 1 changed file with 42 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.activity.OnBackPressedCallback;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -365,6 +366,47 @@ public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathC
final String javascriptVariableName = "android";
vw.addJavascriptInterface(new JavascriptBridge(this), javascriptVariableName);

getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {

if (vw.canGoBack()) {
vw.goBack();
} else {
// To avoid unexpected behaviour, we prompt users and force the app process
// to exit which helps with preserving phone's resources by shutting down
// all goroutines.
//
// Without forced app process exit, some goroutines may remain active even after
// the app resumption at which point new copies of goroutines are spun up.
// Note that this is different from users tapping on "home" button or switching
// to another app and then back, in which case no extra goroutines are created.
//
// A proper fix is to make the backend process run in a separate system thread.
// Until such solution is implemented, forced app exit seems most appropriate.
//
// See the following for details about task and activity stacks:
// https://developer.android.com/guide/components/activities/tasks-and-back-stack
new AlertDialog.Builder(MainActivity.this)
.setTitle("Close BitBoxApp")
.setMessage("Do you really want to exit?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Util.quit(MainActivity.this);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}

}
});

try {
String data = readRawText(getAssets().open("web/index.html"));
vw.loadDataWithBaseURL(BASE_URL, data, null, null, null);
Expand Down Expand Up @@ -615,39 +657,4 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
}
}

// The app cannot currently handle the back button action to allow users
// to move between screens back and forth. What happens is the app is "moved"
// to background as if "home" button were pressed.
// To avoid unexpected behaviour, we prompt users and force the app process
// to exit which helps with preserving phone's resources by shutting down
// all goroutines.
//
// Without forced app process exit, some goroutines may remain active even after
// the app resumption at which point new copies of goroutines are spun up.
// Note that this is different from users tapping on "home" button or switching
// to another app and then back, in which case no extra goroutines are created.
//
// A proper fix is to make the backend process run in a separate system thread.
// Until such solution is implemented, forced app exit seems most appropriate.
//
// See the following for details about task and activity stacks:
// https://developer.android.com/guide/components/activities/tasks-and-back-stack
@Override
public void onBackPressed() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Close BitBoxApp")
.setMessage("Do you really want to exit?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Util.quit(MainActivity.this);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}

0 comments on commit 772821e

Please sign in to comment.