Back button triggering stopped unexpectedly

I've had an incredibly difficult time getting the below app to not show an error when the back button is pressed. All I want is the site to load a web page and the back button to perform as it normally does. Does anyone see anything wrong here? The LogCat shows no errors and with the emulator it doesn't break, but it always breaks on my tablet.

MyWebViewClient.java

package com.MySite;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

MySite.java

package com.mysite;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;

public class MySite extends Activity {
    WebView DCWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //init webview
        WebView DCWebView = (WebView) findViewById(R.id.webview);
        DCWebView.getSettings().setJavaScriptEnabled(true);
        DCWebView.loadUrl("http://mobile.mysite.com");

        //when a link is clicked, use the WebView instead of opening a new browser
        DCWebView.setWebViewClient(new MyWebViewClient());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && DCWebView.canGoBack()) {
            DCWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

Answers


You are declaring a local variable called DCWebView inside of onCreate(). Change the line

WebView DCWebView = (WebView) findViewById(R.id.webview);

to

DCWebView = (WebView) findViewById(R.id.webview);

Then, when the back key is pressed, there will be a back stack (which is currently false when you call DCWebView.canGoBack(), since your global DCWebView was never initialized), the back button will be handled the way you are expecting.


What error are you getting when you hit back on a tablet? Have you tried overriding onBackPressed instead of onKeyDown?


Need Your Help

Requirements Gathering

software-engineering requirements-management

How do you go about the requirements gathering phase? Does anyone have a good set of guidelines or tips to follow? What are some good questions to ask the stakeholders?

Sails: How to return a value from the find function

javascript node.js return sails.js waterline

I am working in sails js. And i want to return the variable sequence[0].NextValue;. But I am not getting it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.