0

I used chromecustomtab in my xamarin android project, i can bind service using CustomTabActivityManger class, but there is no option for unbind the service in that class.

and as i am not unbinding, it always throws me error of memory leak.

I am using Nuget - xamarin.Android.Support.CustomTabs version 26.1.0.1

code is as below

namespace Android.Support.CustomTabs
{
  public class CustomTabsActivityManager
  {
    public CustomTabsActivityManager(Activity parentActivity);

    public CustomTabsSession Session { get; }
    public Activity ParentActivity { get; }
    public CustomTabsClient Client { get; }

    public event CustomTabsServiceDisconnectedDelegate CustomTabsServiceDisconnected;
    public event CustomTabsServiceConnectedDelegate CustomTabsServiceConnected;
    public event ExtraCallbackDelegate ExtraCallback;
    public event NavigationEventDelegate NavigationEvent;

    public static CustomTabsActivityManager From(Activity parentActivity, string servicePackageName = null);
    public bool BindService(string servicePackageName = null);
    public void LaunchUrl(string url, CustomTabsIntent customTabsIntent = null);
    public bool MayLaunchUrl(string url, Bundle extras, List<string> otherLikelyUrls);
    public bool Warmup(long flags = 0);

    public class ExtraCallbackEventArgs
    {
        public ExtraCallbackEventArgs();

        public string CallbackName { get; set; }
        public Bundle Args { get; set; }
    }

    public delegate void NavigationEventDelegate(int navigationEvent, Bundle extras);
    public delegate void ExtraCallbackDelegate(object sender, ExtraCallbackEventArgs e);
    public delegate void CustomTabsServiceConnectedDelegate(ComponentName name, CustomTabsClient client);
    public delegate void CustomTabsServiceDisconnectedDelegate(ComponentName name);
}
York Shen
  • 9,014
  • 1
  • 16
  • 40
Query
  • 625
  • 1
  • 7
  • 22

1 Answers1

1

I can bind service using CustomTabActivityManger class, but there is no option for unbind the service in that class

Analyze:

Usually we could directly use unbindService to unbind the Service. But in the CustomTabActivityManger source code we could found there no UnBindService() method. And you can't get the CustomTabsServiceConnection instance from outside so that it's hard to unbind the service in your Activity:

public class CustomTabsActivityManager
{
    ...
    CustomTabsServiceConnectionImpl connection;
    ...

    public bool BindService (string servicePackageName = null)
    {
        ...

        connection = new CustomTabsServiceConnectionImpl {
            CustomTabsServiceConnectedHandler = (name, client) => {
                Client = client;
                var evt = CustomTabsServiceConnected;
                if (evt != null)
                    evt (name, client);
            },
            OnServiceDisconnectedHandler = (name) => {
                var evt = CustomTabsServiceDisconnected;
                if (evt != null)
                    evt (name);
            }
        };

        return CustomTabsClient.BindCustomTabsService (ParentActivity, servicePackageName, connection);
    }
} 

class CustomTabsServiceConnectionImpl : CustomTabsServiceConnection
{
     ...
}

Solution:

You could create a custom CustomTabsActivityManager class and add the UnBindService() method:

public class MyCustomTabsActivityManager
{
     CustomTabsServiceConnectionImpl connection;

     public Activity ParentActivity { get; private set; }
     public CustomTabsClient Client { get; private set; }

     CustomTabsSession session = null;

     ...

     public void UnBindService()
     {
         if (connection != null)
         {
             ParentActivity.UnbindService(connection);
             Client = null;
             session = null;
          }
     }
}

Then, you could use this UnBindService() in your Activity:

protected override void OnDestroy()
{
    myCustomTabsActivityManager.UnBindService();
    base.OnDestroy();
}
Community
  • 1
  • 1
York Shen
  • 9,014
  • 1
  • 16
  • 40
  • Thanks for your answer ,but my CustomTabsActivityManager class just have public bool BindService(string servicePackageName = null); to bind the service so i got confused what to pass as connection at the time of UnbindService. i added my code snapped in question. – Query Nov 27 '18 at 16:48
  • @Query, create a your own `MyCustomTabsActivityManager` class which extends from `CustomTabsActivityManager `, then create the `UnbindService()` method and pass the connection parameter. – York Shen Nov 28 '18 at 01:34
  • @Query, I have wrote it in the Solution part of my answer. – York Shen Nov 28 '18 at 01:35
  • I tried it but there is some issue with extends, as i am using visual studio and i am not able to extend class , did you ever try with visual studio with c# ? – Query Dec 06 '18 at 20:02
  • @Query, I have tested it on my side without any issue, what is the "class" you can't extend? – York Shen Dec 07 '18 at 03:58
  • I tried to exted CustomTabsActivityManager, if you have working demo thn can you please pass it to me – Query Dec 13 '18 at 22:49