Forum Xamarin.Android

How to Refresh a WebView when Tab is Already Selected (OnTabReselected) ???

hexagodhexagod Member ✭✭✭

what's up xamarin fam,
I'm trying to refresh a WebView when the user has already selected a Tab w/ deprecated TabHost / ActionBar using Xamarin.Android.

Here's my code:

    public void OnTabReselected(SpeakersActivity tab, FragmentTransaction ft)
    {
        WebView subWebView = FindViewById<WebView>(Resource.Id.webViewSubs);
        //tell webview to reload
        subWebView.GoBack();
    }

and I've tried putting that code into both the MainActivity and inside my SpeakersActivity. It doesn't crash my app but when I tap the Tab it still does nothing.

Here's my complete code

https://github.com/hexag0d/BitChute_Mobile_Android_a2/blob/2.68/MainActivity.cs

please help, very hard to find the answer for this

Best Answers

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    You can try ActionBar. And the TabActivity class itself you used in your sample has been deprecated in 4.0.

    The ActionBar has provided the following methods :

            public event EventHandler<TabEventArgs> TabReselected;
            public event EventHandler<TabEventArgs> TabUnselected;
            public event EventHandler<TabEventArgs> TabSelected;
    

    The attachment is a sample which has implement the function of your sample .

    For more details: https://developer.xamarin.com/guides/android/user_interface/tab_layout/actionbar/

  • hexagodhexagod Member ✭✭✭
    edited October 2018 Accepted Answer

    First off thank you so much to @BillyLiu !!! dude is a lifesaver. He knows his Xamarin. Alright, so here's how you can refresh a tab OnClick in Xamarin

    //Assemblies

    using System;
    using System.Threading.Tasks;
    using Android.App;
    using Android.Content;
    using Android.Graphics.Drawables;
    using Android.OS;
    using Android.Widget;
    using Android.Webkit;
    using static Android.Widget.TabHost;
    using static Android.Views.View;
    using Android.Views;

    //this assembly is your Activities class, which should contain your class instances
    using com.xamarin.example.BitChute.Activities;

    Put these click listeners inside your MainActivity.cs .. the int inside GetChildAt() represents the tab our listener will respond to. int 0 = tab farthest left and they go up 0 .. 1 .. 2 .. etc .. 1 would mean tab second from the left. Each listener will need it's own listener class instance. I'll show two examples in the next block.

        TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
    
        tabHost.Setup();
    
        tabHost.TabWidget.GetChildAt(0).SetOnClickListener(new WhatsOnClickListener(tabHost));
    
        tabHost.TabWidget.GetChildAt(1).SetOnClickListener(new SubsClickListener(tabHost));
    
        tabHost.TabWidget.GetChildAt(2).SetOnClickListener(new DiscoverClickListener(tabHost));
    
        tabHost.TabWidget.GetChildAt(3).SetOnClickListener(new MyChannelClickListener(tabHost));
    
        tabHost.TabWidget.GetChildAt(4).SetOnClickListener(new SettingsClickListener(tabHost));
    

    Create a separate ClickActivity.cs file and then add these listeners into it. you will need to create an OnClickListener instance for each tab. This example is only two of the listeners; but if you're using my template, you will need to use these examples to create another 3 listeners.

    public class WhatsOnClickListener : Java.Lang.Object, IOnClickListener
    {
    TabHost tabHost;

    //this int will tell the click listener whether to reload the webview or pop 2 root
    static int tbC = 0;
    
    public WhatsOnClickListener(TabHost tabHost)
    {
        //tell the clicklistener which tabhost to use
        this.tabHost = tabHost;
    }
    
    //this class handles the click event
    public void OnClick(View v)
    {
    
        //declare the webview and tell our object where to find the XAML resource
        WebView webViewWhatsOn = tabHost.CurrentView.FindViewById<WebView>(Resource.Id.webViewWhatsOn);
    
        //...if the CurrentTab != 0 ... we won't fire the Reload() or LoadUrl() 
        //..without this logic, the app will crash because our WebViews 
        //.aren't set to an instance of an object
        if (tabHost.CurrentTab == 0)
        {
            //if tbC int is 0, we will reload the page
            if (tbC == 0)
            {
                //tell whatsOnWebView to Reload
                webViewWhatsOn.Reload();
    
                //set the int to one so next time webview will pop to root
                tbC = 1;
            }
            //else if the int is 1, we will pop to root on tab 0
            else if (tbC == 1)
            {
                //tell whatsOnWebView to pop to root
                webViewWhatsOn.LoadUrl(@"https://bitchute.com/");
    
                //set the tbC int so that next time webview will reload
                tbC = 0;
            }
        }
    
        //if our current tab isn't zero, we need to set CurrentTab to 0
        //this line is critical or our tabs won't work when not selected
        tabHost.CurrentTab = 0;
    }
    

    }

    public class SubsClickListener : Java.Lang.Object, IOnClickListener
    {
    TabHost tabHost1;

    static int tbC = 0;
    
    public SubsClickListener(TabHost tabHost1)
    {
        this.tabHost1 = tabHost1;
    }
    public void OnClick(View v)
    {
        if (tabHost1.CurrentTab == 1)
        {
    
            WebView subWebView = tabHost1.CurrentView.FindViewById<WebView>(Resource.Id.webViewSubs);
    
            if (tbC == 0)
            {
                subWebView.Reload();
    
                tbC = 1;
            }
    
            else if (tbC == 1)
            {
                subWebView.LoadUrl(@"https://bitchute.com/subscriptions/");
    
                tbC = 0;
            }
        }
    
        tabHost1.CurrentTab = 1;
    }
    

    }

    hope that helps! I've been trying this for a minute.

Answers

  • hexagodhexagod Member ✭✭✭

    lol that is me asking... and his answer is OK but the problem is it only works if the tab is already loaded (and thus, it doesn't work properly). If the tab hasn't been loaded yet I get object reference not set to an instance of an object

        public class MyOnClickListener : Java.Lang.Object, IOnClickListener
        {
            TabHost tabHost;
            public MyOnClickListener(TabHost tabHost)
            {
                this.tabHost = tabHost;
            }
    
            public void OnClick(View v)
            {
    
                WebView subWebView = tabHost.CurrentView.FindViewById<WebView>(Resource.Id.webViewSubs);
    
                if (subWebView != null)
                {
                    subWebView.Reload();
                }
    
    
            }
        }
    
                TabHost.TabWidget.GetChildAt(0).SetOnClickListener(new MyOnClickListener(tabHost));
    
  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    You can try ActionBar. And the TabActivity class itself you used in your sample has been deprecated in 4.0.

    The ActionBar has provided the following methods :

            public event EventHandler<TabEventArgs> TabReselected;
            public event EventHandler<TabEventArgs> TabUnselected;
            public event EventHandler<TabEventArgs> TabSelected;
    

    The attachment is a sample which has implement the function of your sample .

    For more details: https://developer.xamarin.com/guides/android/user_interface/tab_layout/actionbar/

  • hexagodhexagod Member ✭✭✭

    Yeah I know it's deprecated but the project is almost finished and it still works in api 8.0 ... I am going to switch to the toolbar in the next release but I'm going to release this version. The actionbar works fine for what I'm trying to do, mostly. Thanks for the tip there; I'll try it when I get home.

  • hexagodhexagod Member ✭✭✭

    @jezh said:
    You can try ActionBar. And the TabActivity class itself you used in your sample has been deprecated in 4.0.

    The ActionBar has provided the following methods :

            public event EventHandler<TabEventArgs> TabReselected;
            public event EventHandler<TabEventArgs> TabUnselected;
            public event EventHandler<TabEventArgs> TabSelected;
      
    

    The attachment is a sample which has implement the function of your sample .

    For more details: https://developer.xamarin.com/guides/android/user_interface/tab_layout/actionbar/

    LOL ... did you just make that? Fricken sweet dude. I'll clean this up tonight and see if it works in 8.0 .. thanks for the help my man!

  • hexagodhexagod Member ✭✭✭

    @jezh you are now a bitchute developer! I'll definitely include you in the credits, you should git on github and do a pull request. I've got other things I'm working on. I'll try and do most of the work but it'd be cool to have another more experienced developer on board.

  • hexagodhexagod Member ✭✭✭
    edited October 2018 Accepted Answer

    First off thank you so much to @BillyLiu !!! dude is a lifesaver. He knows his Xamarin. Alright, so here's how you can refresh a tab OnClick in Xamarin

    //Assemblies

    using System;
    using System.Threading.Tasks;
    using Android.App;
    using Android.Content;
    using Android.Graphics.Drawables;
    using Android.OS;
    using Android.Widget;
    using Android.Webkit;
    using static Android.Widget.TabHost;
    using static Android.Views.View;
    using Android.Views;

    //this assembly is your Activities class, which should contain your class instances
    using com.xamarin.example.BitChute.Activities;

    Put these click listeners inside your MainActivity.cs .. the int inside GetChildAt() represents the tab our listener will respond to. int 0 = tab farthest left and they go up 0 .. 1 .. 2 .. etc .. 1 would mean tab second from the left. Each listener will need it's own listener class instance. I'll show two examples in the next block.

        TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
    
        tabHost.Setup();
    
        tabHost.TabWidget.GetChildAt(0).SetOnClickListener(new WhatsOnClickListener(tabHost));
    
        tabHost.TabWidget.GetChildAt(1).SetOnClickListener(new SubsClickListener(tabHost));
    
        tabHost.TabWidget.GetChildAt(2).SetOnClickListener(new DiscoverClickListener(tabHost));
    
        tabHost.TabWidget.GetChildAt(3).SetOnClickListener(new MyChannelClickListener(tabHost));
    
        tabHost.TabWidget.GetChildAt(4).SetOnClickListener(new SettingsClickListener(tabHost));
    

    Create a separate ClickActivity.cs file and then add these listeners into it. you will need to create an OnClickListener instance for each tab. This example is only two of the listeners; but if you're using my template, you will need to use these examples to create another 3 listeners.

    public class WhatsOnClickListener : Java.Lang.Object, IOnClickListener
    {
    TabHost tabHost;

    //this int will tell the click listener whether to reload the webview or pop 2 root
    static int tbC = 0;
    
    public WhatsOnClickListener(TabHost tabHost)
    {
        //tell the clicklistener which tabhost to use
        this.tabHost = tabHost;
    }
    
    //this class handles the click event
    public void OnClick(View v)
    {
    
        //declare the webview and tell our object where to find the XAML resource
        WebView webViewWhatsOn = tabHost.CurrentView.FindViewById<WebView>(Resource.Id.webViewWhatsOn);
    
        //...if the CurrentTab != 0 ... we won't fire the Reload() or LoadUrl() 
        //..without this logic, the app will crash because our WebViews 
        //.aren't set to an instance of an object
        if (tabHost.CurrentTab == 0)
        {
            //if tbC int is 0, we will reload the page
            if (tbC == 0)
            {
                //tell whatsOnWebView to Reload
                webViewWhatsOn.Reload();
    
                //set the int to one so next time webview will pop to root
                tbC = 1;
            }
            //else if the int is 1, we will pop to root on tab 0
            else if (tbC == 1)
            {
                //tell whatsOnWebView to pop to root
                webViewWhatsOn.LoadUrl(@"https://bitchute.com/");
    
                //set the tbC int so that next time webview will reload
                tbC = 0;
            }
        }
    
        //if our current tab isn't zero, we need to set CurrentTab to 0
        //this line is critical or our tabs won't work when not selected
        tabHost.CurrentTab = 0;
    }
    

    }

    public class SubsClickListener : Java.Lang.Object, IOnClickListener
    {
    TabHost tabHost1;

    static int tbC = 0;
    
    public SubsClickListener(TabHost tabHost1)
    {
        this.tabHost1 = tabHost1;
    }
    public void OnClick(View v)
    {
        if (tabHost1.CurrentTab == 1)
        {
    
            WebView subWebView = tabHost1.CurrentView.FindViewById<WebView>(Resource.Id.webViewSubs);
    
            if (tbC == 0)
            {
                subWebView.Reload();
    
                tbC = 1;
            }
    
            else if (tbC == 1)
            {
                subWebView.LoadUrl(@"https://bitchute.com/subscriptions/");
    
                tbC = 0;
            }
        }
    
        tabHost1.CurrentTab = 1;
    }
    

    }

    hope that helps! I've been trying this for a minute.

Sign In or Register to comment.