Android webview back button help

  • Replies:0
  • OpenNot stickiedUnanswered

Nov 18, 2011 11:14:58 AM via Website

Hi,

I cannot seem to get the back button to work in my app, for the back code i get a red underline under the mwebview which on hover says it cannot be resolved.

My code is below, can someone tell me where im going wrong:

1package com.mysite.myapp;
2
3import android.app.Activity;
4import android.content.Intent;
5import android.os.Bundle;
6import android.view.KeyEvent;
7import android.view.View;
8import android.view.Window;
9import android.webkit.WebChromeClient;
10import android.webkit.WebView;
11import android.webkit.WebViewClient;
12import android.widget.Button;
13
14publicclass myappActivity extends Activity
15{
16
17final Activity activity = this;
18
19
20@Override
21publicboolean onKeyDown(int keyCode, KeyEvent event) {
22if ((keyCode == KeyEvent.KEYCODE_BACK) && mwebView.canGoBack()) {
23mwebView.goBack();
24returntrue;
25}
26returnsuper.onKeyDown(keyCode, event);
27}
28
29
30
31
32@Override
33publicvoid onCreate(Bundle savedInstanceState)
34{
35super.onCreate(savedInstanceState);
36
37this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
38setContentView(R.layout.main);
39
40final WebView mwebView = (WebView) findViewById(R.id.webview);
41mwebView.getSettings().setJavaScriptEnabled(true);
42
43mwebView.setWebChromeClient(new WebChromeClient() {
44publicvoid onProgressChanged(WebView view, int progress)
45{
46activity.setTitle("Loading...");
47activity.setProgress(progress * 100);
48
49if(progress == 100)
50activity.setTitle(R.string.app_name);
51}
52
53});
54
55mwebView.setWebViewClient(new WebViewClient() {
56@Override
57publicvoid onReceivedError(WebView view, int errorCode, String description, String failingUrl)
58{
59// Handle the error
60}
61@Override
62publicboolean shouldOverrideUrlLoading(WebView view, String url)
63{
64view.loadUrl(url);
65returntrue;
66}
67});
68
69mwebView.loadUrl(http://www.mysite.com);
70
71Button webButton = (Button) findViewById(R.id.button1);
72webButton.setOnClickListener(new OnClickListener() {
73@Override
74publicvoid onClick(View v) {
75mwebView.loadUrl(http://www.mysite.com/page1.php);
76}});
77
78Button webButton1 = (Button) findViewById(R.id.button2);
79webButton1.setOnClickListener(new OnClickListener() {
80@Override
81publicvoid onClick(View v) {
82mwebView.loadUrl("http://www.mysite.com/page2.php");
83}});
84
85Button webButton2 = (Button) findViewById(R.id.button3);
86webButton2.setOnClickListener(new OnClickListener() {
87@Override
88publicvoid onClick(View v) {
89mwebView.loadUrl("http://www.mysite.com/page3.php");
90}});
91
92Button webButton3 = (Button) findViewById(R.id.button4);
93webButton3.setOnClickListener(new OnClickListener() {
94@Override
95publicvoid onClick(View v) {
96mwebView.loadUrl("http://www.mysite.com/page4.php");
97}});
98
99Button webButton4 = (Button) findViewById(R.id.button5);
100webButton4.setOnClickListener(new View.OnClickListener() {
101publicvoid onClick(View view) {
102Intent myIntent = new Intent(view.getContext(), morescreen.class);
103startActivityForResult(myIntent, 0);
104}
105
106});
107
108Button webButton5 = (Button) findViewById(R.id.button6);
109webButton5.setOnClickListener(new OnClickListener() {
110@Override
111publicvoid onClick(View v) {
112mwebView.loadUrl("http://www.mysite.com/page5.php");
113}});
114
115}
116
117}

Im very new to Android, in fact this is my first app so apologies if the answer is really simple.

Thanks in advance

Jay