title: Get current Activity - Xamarin Android
body: <p>I am developing an portable App for Android and iOS. My current function is taking a Screenshot and use that image in the code. Therefor I have an Interface in the portable library.</p>
  
  <pre><code>public interface IFileSystemService
  {
      string GetAppDataFolder();
  }
  </code></pre>
  
  <p>I am taking the Screenshot also in the portable Library with the following code:</p>
  
  <pre><code>static public bool TakeScreenshot()
      {
          try
          {
              byte[] ScreenshotBytes = DependencyService.Get&lt;Interface.IScreenshotManager&gt;().TakeScreenshot();
              return true;
          }
          catch (Exception ex)
          {
          }
          return false;
      }
  </code></pre>
  
  <p>This either calls the Android or the iOS version. </p>
  
  <p>Android:</p>
  
  <pre><code>class ScreenshotManagerAndroid : IScreenshotManager
  {
      public static Activity Activity { get; set; }
  
      public byte[] TakeScreenshot()
      {
  
          if (Activity == null)
          {
              throw new Exception("You have to set ScreenshotManager.Activity in your Android project");
          }
  
          var view = Activity.Window.DecorView;
          view.DrawingCacheEnabled = true;
  
          Bitmap bitmap = view.GetDrawingCache(true);
  
          byte[] bitmapData;
  
          using (var stream = new MemoryStream())
          {
              bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
              bitmapData = stream.ToArray();
          }
  
          return bitmapData;
      }
  </code></pre>
  
  <p>The question now is to get the current Activity from my app.</p>