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 6622239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:22:51+00:00 2026-05-25T21:22:51+00:00

I have a webview as the main activity and I want refresh the webview

  • 0

I have a webview as the main activity and I want refresh the webview from the “Preference” (listview) that is from another activity. Is it possible to do that? I don’t want to have the “refresh” button on the apps menu.

But the activity will crash after i pressed “refresh” on my preference activity , i assume that is “R.id.web_engine” , that is from the MainActivity layout , that causes the crash (look at the code below).

How can I perform a webview action from the external activity ?

Example :

In my MainActivity will have

        //Webview
        final WebView engine = (WebView) findViewById(R.id.web_engine);
        engine.loadUrl("file:///android_asset/www/index.html");
        engine.getSettings().setJavaScriptEnabled(true);

and Preferences

//Get the custom preference
Preference refreshPref = (Preference) findPreference("refreshPref");
refreshPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

             public boolean onPreferenceClick(Preference preference) {

         Toast.makeText(getBaseContext(),
         "Loading...",Toast.LENGTH_SHORT).show();
         WebView engine = (WebView) findViewById(R.id.web_engine);
         engine.loadUrl("javascript:window.location.reload();");

         return true;
                      }

              });
  • 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-05-25T21:22:51+00:00Added an answer on May 25, 2026 at 9:22 pm

    I agree with Rahul Choudhary. Your webview is only visible from your main activity. You have to pass it a message that it should refresh next time it is visible.

    The right place for this code is on the main activity onResume() method.
    You can use intents to launch it again (it will be recreated and loose its history) or use SharedPreferences that are accessible by both activities.

    EDIT: added some code below.

    MainActivity

    //onCreate
    final WebView engine = (WebView) findViewById(R.id.web_engine);
    engine.loadUrl("file:///android_asset/www/index.html");
    engine.getSettings().setJavaScriptEnabled(true);
    
    //onResume
    boolean refresh = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("SHOULD_REFRESH", false)
    if (refresh) {
      engine.loadUrl("javascript:window.location.reload();");
      //remove the shared preference here or set it to false to prevent reloading next time
    }
    

    and Preferences

    //Get the custom preference
    Preference refreshPref = (Preference) findPreference("refreshPref");
    refreshPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {
                   PreferenceManager.getDefaultSharedPreferences(this).edit().putBoolean("SHOULD_REFRESH", true).commit();             
                   return true;
                          }
    
                  });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a webview and I want to manually switch to another tab during
What i have here is a simple webview activity that when loaded it auto
I want to load .flv video in webview. I have taken help from this
I'm using a webview on my activity. page is loaded from android_asset: webView.loadUrl(file:///android_asset/htm/main.htm); I
I have a WebView. I'd like to show some page from my server, but
I have a webview, which shows the pages of a ebook. I want to
I have a Cocoa app that uses a WebView to display an HTML interface.
I have a NSWindow that hosts a WebView that Ive hooked up to a
My Android app has a main WebView (HTML loaded from a local resource) which
I have an Android application that is a TabHost with a WebView. I use

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.