Reputation: 2287
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.
Upvotes: 0
Views: 457
Reputation: 13303
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 IntentFilter
s.
Upvotes: 2