Search code examples
androidandroid-intentandroid-webviewandroid-tabhost

Web Page is not Available error in WebView


I am creating the application using the following tutorial

http://www.coderzheaven.com/2011/05/09/android-tabbars-example/

For every tab i have added one layout with webview. I want to check Network availability for every tab changing, and i have redirected the page to errorActivity if net connection is not available.

import android.app.TabActivity;
import android.content.*;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class MyView extends TabActivity implements OnTabChangeListener
{   
    TabHost tabHost;
    String value;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources(); 
        tabHost = (TabHost)findViewById(android.R.id.tabhost);


        TabSpec FirstTabSpec = tabHost.newTabSpec("tab1");
        TabSpec SecondTabSpec = tabHost.newTabSpec("tab2");
        TabSpec ThirdTabSpec = tabHost.newTabSpec("tab3");
        TabSpec FourthTabSpec = tabHost.newTabSpec("tab4");


        FirstTabSpec.setIndicator("Tab1",res.getDrawable(R.drawable.tab1)).setContent(new Intent(this,FirstView.class));
        secondTabSpec.setIndicator("Tab2 ",res.getDrawable(R.drawable.tab2)).setContent(new Intent(this,SecondView.class));
        ThirdTabSpec.setIndicator("Tab3",res.getDrawable(R.drawable.tab3)).setContent(new Intent(this,ThirdView.class));
        FourthTabSpec.setIndicator("Tab4",res.getDrawable(R.drawable.tab4)).setContent(new Intent(this,FourthView.class));


        tabHost.addTab(FirstTabSpec);
        tabHost.addTab(SecondTabSpec);
        tabHost.addTab(ThirdTabSpec);
        tabHost.addTab(FourthTabSpec);
        tabHost.setOnTabChangedListener(this);
    }

    public void onTabChanged(String tabId) 
    {   
        if(!IsNetworkAvaialble())
        {

                webview.stopLoading();
                Intent myIntent = new Intent((Activity)MyView.this, NetErrorPage.class);   
            myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            ((Activity)MyView.this).startActivity(myIntent);
            finish();
        }

    }

}

But clicking on the tab, page is redirected to the error page before that 2 or 3 seconds the "Web Page Not Available" is displaying. How to stop this?


Solution

  • I have solved this issue as below.

    In OnTabchanged event, i have set one static boolean value as true. In each activity class i have included loadurl method. Now i have changed this if the netconnection is failed and the static boolean value is not equal to true, then i have stopped the loading and displayed the alert. Otherwise loaded the url.