S
S
sigarew2018-10-17 12:46:17
Android
sigarew, 2018-10-17 12:46:17

When I press back android application closes, what should I do?

Hello! My daughter was given homework to create an application from a mobile site. The application has been created, but there is a problem - when you click back, the application closes, but you need to go to the previous page. Here is the code:
Main Activity:

package ru.yandex;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.*;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* разворачиваем приложение на весь экран */
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        /* применяем наш лейаут к текущему экрану */
        setContentView(R.layout.activity_main);

        /* находим WebView элемент по его id */
        WebView webView = (WebView) findViewById(R.id.WebView);

        /* создаем новые настройки для нашего WebView элемента */
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccess(true);
        webView.setWebViewClient(new MyClient());

        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        /* здесь вы можете поместить URL вашего сайта */
        webView.loadUrl("http://yandex.ru/");
    }

}

class MyClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        /* откроем новую веб-страницу в webview */
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        /*остановка загрузки и отображение страницы error.html из папки “assets”*/
        view.stopLoading();
        view.loadUrl(String.format("file:///android_asset/error.html?code=%s&description=%s&url=%s", Uri.encode(String.valueOf(errorCode)), Uri.encode(description), Uri.encode(failingUrl)));
    }
}

Manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ru.yandex" >
    <uses-permission android:name="android.permission.INTERNET"/>

    <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/AppTheme">
        <activity android:name="ru.yandex.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2018-10-17
@sigarew

You probably need this line:

@Override
    public void onBackPressed() {
        //вебвью наверное всё же стоит объявить в классе в не ка локальную переменную
        webview.goBack();
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question