1. Download our Official Android App: Forums for Android!

Apps Webview go back help please

Discussion in 'Android Development' started by jaylam, Nov 17, 2011.

  1. jaylam

    jaylam Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Nov 4, 2011

    Nov 4, 2011
    2
    0
    5
    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:

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

    Thanks in advance

    Jay
     

    Advertisement

Share This Page

Loading...