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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:59:15+00:00 2026-06-01T07:59:15+00:00

I have the following problem. I have a tabbed application implemented using a ViewPager.

  • 0

I have the following problem. I have a tabbed application implemented using a ViewPager. Each tab is a Fragment with nothing but a webview in it. I have an inner class BroadcastReceiver that catches changes to the internet connection. What I want to do is reload the webpage when it picks up the fact that the internet has reconnected. The problem is that the inner class is static and so I can’t all non-static methods like mWebView.Reload();

Is it possible to get around this problem?

Below is the code for the Fragment that contains the webview. I have 7 different tabs and this class is instantiated all 7 times.

public class MyWebviewFragment extends SherlockFragment
{
final static private String tag = MyWebviewFragment.class.getSimpleName();
String mTabURL;
private WebView mWebView = null;
static final int REFRESH_ID = Menu.FIRST;
private View v;
private int mTabNumber=-1;
private boolean mWebViewLoaded=false;

public static class ConnectionChangeReceiver extends BroadcastReceiver
{
  @Override
  public void onReceive( Context context, Intent intent )
  {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    //NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE );
    if ( activeNetInfo != null )
    {
        Toast toast = Toast.makeText(context, R.string.internet_reconnected, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

    }
    else
    {
        Toast toast = Toast.makeText(context, R.string.internet_not_on, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
  }
}

/**
 * When creating, retrieve this instance's number from its arguments.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(tag,"onCreate");

    super.onCreate(savedInstanceState);

    // Tell the framework to try to keep this fragment around
    // during a configuration change.
    setRetainInstance(true);

    if(getArguments() != null)
    {
        mTabURL = getArguments().getString("tabURL");
        mTabNumber = getArguments().getInt("tabNumber");
    }
    Log.i(tag,"***** Exiting onCreate: tab#: " + mTabNumber + " tab URL: " + mTabURL + " *****");
}

/**
 * The Fragment's UI is just a simple text view showing its
 * instance number.
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    boolean bSavedInstanceStateRestored = false;
    Log.i(tag,"onCreateView " + "tab#: " + mTabNumber);

    // Create view object to return
    v = inflater.inflate(R.layout.webview_layout, container, false);

    // Check to see if it has been saved and restore it if true
    if(savedInstanceState != null)
    {
        Log.i(tag,"savedInstance != null in onCreateView - attempting to restore view");

        if (savedInstanceState.isEmpty())
            Log.i(tag, "Can't restore state because bundle is empty.");
        else
        {
            mWebView = ((WebView)v.findViewById(R.id.webview_fragment));
            if(mWebView.restoreState(savedInstanceState) == null)
            {
                Log.i(tag, "Restoring state FAILED!");
            }
            else
            {
                Log.i(tag, "*!*!*!*!*!*! Restoring state succeeded. *!*!*!*!*!*!");
                initWebView();
                bSavedInstanceStateRestored = true;
            }
        }

    }

    if(!bSavedInstanceStateRestored)
    {
        Log.i(tag,"savedInstance == null in onCreateView - creating new webview");
        // Load web page
        mWebView = (WebView)v.findViewById(R.id.webview_fragment);
        initWebView();
        mWebView.loadUrl(mTabURL);

    }
    Log.i(tag, "***** Exiting onCreateView " + "tab#: " + mTabNumber + " *****");
    return v;
}

void initWebView()
{
    Log.i(tag, "initWebView " + "tab#: " + mTabNumber);

    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.setOnKeyListener(new OnKeyListener(){
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            }
            return false;
        }

    });

    // Used to be in new creation
    mWebView.setWebViewClient(new MyWebViewClient());
    mWebView.getSettings().setBuiltInZoomControls(false);
    mWebView.getSettings().setSupportZoom(false);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setDomStorageEnabled(true);

    Log.i(tag, "***** Exit initWebView " + "tab#: " + mTabNumber + " *****");
}
...

Alternatively, I would like to programmatically change back to the first tab (tab 0) and this would redraw automatically.

  • 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-01T07:59:17+00:00Added an answer on June 1, 2026 at 7:59 am

    I’m not certain, but I’ll assume for now that the reason you’ve declared ConnectionChangeReceiver to be static is so that you can register the receiver and its filters in AndroidManifest.xml.

    Since the behavior of the receiver is specific to the instance of the Fragment it should not be static. Rather than using the AndroidManifest.xml way of registering the receiver, make the receiver class non-static and create and register the receiver in the Fragment‘s onActivityCreated method using Context.registerReceiver() and the appropriate IntentFilters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following problem. I have created a tab based Application with three Views
I have following problem: I have built a tabbar application with 4 tabs. I
I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem in my Data Structures and Problem Solving using Java
I have the following problem: I select data from a DB, and for each
I have following problem: My webservice application returns xml data in following order: <my_claims>
I have following problem. I worked two days on a solution but I cannot
I have following problem. I have some unit tests implemented in a foreign assebly
I have following problem with using cucumber and jRuby under windows: load error: bundler/definition

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.