Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:15:45+00:00 2026-06-12T09:15:45+00:00

My android application has a clickable welcome page which leads to a webview with

  • 0

My android application has a clickable welcome page which leads to a webview with multiple icons. when i click on icon it opens a HTML file which is stored in the assets folder. Similarly, clicking on other icons leads to opening of HTML resources in same webview instance. my problem is that if lets suppose i am on third level of navigation and i press android device back button, i get back to welcome page (first level navigation). How can i get to second level of navigation where my all pages icons are present, if i press back button in third level of navigation. My code for second level navigation is as below:

public class Home extends Activity {



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        WebView webview = (WebView) findViewById(R.id.webView1);
       webview.loadUrl("file:///android_asset/webpages/index.html");
       webview.getSettings().setBuiltInZoomControls(true);
       webview.getSettings().setSupportZoom(true);
       webview.getSettings().setJavaScriptEnabled(true);
    }


}

index.html contains icons for navigating to different pages like contact.html, feedback.html
if let suppose i am in contact.html and i press back button on android i should reach to index.html rather than first screen of my application.

@Zyber i am new to java and android development. i tried your suggestion, but when i press back button, i get force closed error.my code is as below :

 public class Home extends Activity {

    private WebView webview;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

                    webview.goBack();

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        WebView webview = (WebView) findViewById(R.id.webView1);
       webview.loadUrl("file:///android_asset/webpages/index.html");
       webview.getSettings().setBuiltInZoomControls(true);
       webview.getSettings().setSupportZoom(true);
       webview.getSettings().setJavaScriptEnabled(true);

    }


}

my logcat code is as below:

0-02 19:01:07.536: E/AndroidRuntime(4160): FATAL EXCEPTION: main
10-02 19:01:07.536: E/AndroidRuntime(4160): java.lang.NullPointerException
10-02 19:01:07.536: E/AndroidRuntime(4160):     at com.summit.mpidc.Home.onKeyDown(Home.java:21)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at android.view.KeyEvent.dispatch(KeyEvent.java:2553)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at android.app.Activity.dispatchKeyEvent(Activity.java:2329)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1825)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3340)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3313)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2473)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at android.os.Looper.loop(Looper.java:137)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at java.lang.reflect.Method.invokeNative(Native Method)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at java.lang.reflect.Method.invoke(Method.java:511)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
10-02 19:01:07.536: E/AndroidRuntime(4160):     at dalvik.system.NativeStart.main(Native Method)

EDITED code:

public class Home extends Activity {


    private WebView webview;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        WebView webview = (WebView) findViewById(R.id.webView1);
       // WebView webview = new WebView(this);
       // setContentView(webview);

       webview.loadUrl("file:///android_asset/webpages/index.html");
       webview.getSettings().setBuiltInZoomControls(true);
       webview.getSettings().setSupportZoom(true);
       webview.getSettings().setJavaScriptEnabled(true);
       webview.getSettings().setUseWideViewPort(true);
       webview.setInitialScale(1);
       webview.canGoBack();

    }

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



}

I am getting null pointer exception.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:15:46+00:00Added an answer on June 12, 2026 at 9:15 am

    If I understand you correctly you could try and override the onKeyDown function and react to the back key press.

    WebView webview;
    public void onCreate(...)
    {
        ...
        webview = (WebView) findViewById(...);
        ...
    }    
    
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack())
        {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    

    This will intercept when you press the back key, and if your webview has a history, it will go one step back in the history. If you are on the first page loaded and press back, it will terminate the webview activity and go back to your welcome activity.

    Edit:

    Yes, you could use onBackPressed as well, I guess it would be more appropriate. As pointed out by njzk2 another approach would be.

    @Override
    public void onBackPressed()
    {
        if(mWebView.canGoBack())
        {
            mWebView.goBack();          
        }
        else
        {
            super.onBackPressed();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android application which has a login screen. Upon the click of
I had developed Android Application which has Login Authentication Page. When the user logs
I am developing the android application in which The application has a stored password
I am making an android application which has a main activity and several other
My android application needs to display time which has to be incremented according to
I'm building an android application, which has a list view, and in the list
i have an android application which has a listview for listing 14 items inside
I have an Android application that has one section loading a web page within
I am currently working on one android application which has C2DM implementation and I
I am writing an android application which has a lot of images in it.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.