1

I am creating an application with webview (an application in which a website is displayed).

Currently, if I click the back button, the app closes the application

What I am looking for is not that a button with an arrow appears in the action bar and it takes me back, because I have eliminated that bar, what I want is to be able to use the buttons on the cell phone.

I hope you can help me, here the code of the MainActivity.java and the AndroidManifiest.xml

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private View decorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = findViewById(R.id.mywebview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new myChrome());
        webView.loadUrl("https://mywebview.com/");

        decorView = getWindow().getDecorView();
        decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                if (visibility  == 0)
                    decorView.setSystemUiVisibility(hideSystemBars());
            }
        });
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            decorView.setSystemUiVisibility(hideSystemBars());
        }
    }

AndroidManifiest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Dessert.decov">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DESSERT">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:configChanges="screenSize|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Franz Dev
  • 11
  • 1
  • Does this answer your question? [How to go back to previous page if back button is pressed in WebView?](https://stackoverflow.com/questions/6077141/how-to-go-back-to-previous-page-if-back-button-is-pressed-in-webview) – OMi Shah Dec 29 '21 at 18:22
  • I did it as is but it still does not work – Franz Dev Dec 29 '21 at 18:28
  • post the complete code after adding the back button part. – OMi Shah Dec 29 '21 at 18:30

0 Answers0