Argardor
Argardor

Reputation: 79

Cannot resolve method onKeyDown

I want to create a back option (for webView), but i have a problem with onKeyDown.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && wv7.canGoBack()) {
        wv7.goBack();
        return true;


}
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }

I have this error : Cannot resolve method 'onKeyDown(int, android.view.KeyEvent)'

Can you help me to resolve it. Thank you very much.

Edit - full class -

public class vod extends Fragment {
    private WebView wv7;
    public static final String TAG = "VOD";
    private VideoEnabledWebView webView;
    private VideoEnabledWebChromeClient webChromeClient;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        View rootView = inflater.inflate(R.layout.vod, container, false);
        // Set layout
        //rootView.findViewById(R.layout.vld);

        // Save the web view
        wv7 = (VideoEnabledWebView) rootView.findViewById(R.id.webView7);

        // Initialize the VideoEnabledWebChromeClient and set event handlers
        View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout); // Your own view, read class comments
        ViewGroup videoLayout = (ViewGroup) rootView.findViewById(R.id.videoLayout); // Your own view, read class comments
        View loadingView = inflater.inflate(R.layout.vod2, null); // Your own view, read class comments
        webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors...
        {
            // Subscribe to standard events, such as onProgressChanged()...
            @Override
            public void onProgressChanged(WebView view, int progress) {
                // Your code...
            }
        };
        webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback() {
            @Override
            public void toggledFullscreen(boolean fullscreen) {
                // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
                if (fullscreen) {
                    WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                    attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    getActivity().getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14) {
                        getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                    }
                } else {
                    WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes();
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    getActivity().getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14) {
                        getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                    }
                }

            }
        });
        wv7.setWebChromeClient(webChromeClient);

        // Navigate everywhere you want, this classes have only been tested on YouTube's mobile site
        wv7.loadUrl("XX");

return rootView;
    }

    @Override
    public void onResume()
    {
        super.onResume();
        wv7.onResume();
    }
    @Override
    public void onPause()
    {
        super.onPause();
        wv7.onPause();
    }
    @Override
    public void onBackPressed() {
        if (wv7.canGoBack()) {
            wv7.goBack();
            return;
        }
        super.onBackPressed();
    }
}

Upvotes: 1

Views: 2836

Answers (3)

Elio Lako
Elio Lako

Reputation: 1351

I would suggest trying this one :

val webSettings = webView.settings
        webSettings.javaScriptEnabled = true
        webSettings.domStorageEnabled = true
        webSettings.setAppCacheEnabled(true)

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    // Check if the key event was the Back button and if there's history
    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
        webView.goBack()
        return true
    }
    return super.onKeyDown(keyCode, event)
}

Upvotes: 1

Sidd
Sidd

Reputation: 198

  1. Please write this inside OnCreateView method

  2. Here wv_loader is WebView id.

    wv_loader.setOnKeyListener(new View.OnKeyListener() {
         @Override
         public boolean onKey(View v, int keyCode, KeyEvent event) {
             //This is the filter
             if (event.getAction()!=KeyEvent.ACTION_DOWN)
                 return true;
             if (keyCode == KeyEvent.KEYCODE_BACK) {
                 if (wv_loader.canGoBack()) {
                     wv_loader.goBack();
                 } else {
                     (getActivity()).onBackPressed();
                 }
                 return true;
             }
             return false;
         }
     });
    

Upvotes: 0

Prasanth S
Prasanth S

Reputation: 3820

Try this

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (wv7.canGoBack()) {
                    wv7.goBack();
                } 
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

or

@Override
public void onBackPressed() {
    if (wv7.canGoBack()) {
        wv7.goBack();
        return;
    }
    super.onBackPressed();
}

Upvotes: 0

Related Questions