Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
For example, I have added the TextView inside the fragment as below.

        var adaptor = new GenericFragmentPagerAdaptor(SupportFragmentManager); // where GenericFragementPagerAdaptor is a sub-class derived from FragementPagerAdaptor

        adaptor.AddFragmentView((i, v, b) => {
            var view = i.Inflate(Resource.Layout.tab, v, false);
            textView = new TextView(view.Context);
            textView.Text = "Test1";
            view.FindViewById<FrameLayout>(Resource.Id.mainFrame).AddView(textView);
            return view;

If I am destroying the current activity means, all the resources related to the fragment should be cleared.Where and how can I do that? Please, anyone suggests me.


What I have tried:

I have tried by given like below in OnDestroyView() and OnDetach() override methods in Fragment class

this.FragmentManager.Fragments.Clear();

But it did not worked.
Posted
Updated 7-Sep-16 13:21pm

1 solution

If i understand what you are asking i think what you want to do is use the IDisposable interface which will handle the cleanup of resources for you.

Your usage having used IDisposable would then be


C#
using(var adaptor = new GenericFragmentPagerAdaptor(SupportFragmentManager))
{

        adaptor.AddFragmentView((i, v, b) => {
            var view = i.Inflate(Resource.Layout.tab, v, false);
            textView = new TextView(view.Context);
            textView.Text = "Test1";
            view.FindViewById<FrameLayout>(Resource.Id.mainFrame).AddView(textView);
            return view;
		});
}


By doing it this way, anything outside the using(){} would be automatically cleaned up by the garbage disposal process.

An example of implementing IDisposable would be something like this:

C#
public class MyClass : IDisposable
{
   private bool Disposed { get; set; }

   public void Dispose()
   {
      this.Dispose(true);
      GC.SuppressFinalize(this);
   }
   
   protected virtual void Dispose(bool disposing)
   {
      if (!this.Disposed)
      {
         if (disposing)
         {
            // do your code cleanup here to free resources and what not
         }
      }

      this.Disposed = true;
   }
}


//usage

using(var cl = new MyClass())
{
   //Run some code using the class here
}

// Disposable method has been run at this point
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900