3

I have a RemoteViews instance, received from another app as parcelable data. I cannot simply change the service result (still have to provide backwards compatibility). The received RemoteViews has a simple layout: a LinearLayout container, with a TextView and an ImageView inside. The image view comes with a Bitmap image set. Up to now, that remote view was inflated by calling remoteView.apply(Context context, ViewGroup parent), and then the resulting view is added to another layout on the screen.

After updating the support library to the latest version at the moment: in my case from 25.3.1 to 26.1.0, calling remoteView.apply(Context, ViewGroup) started throwing an exception:

android.widget.RemoteViews$ActionException: view: android.support.v7.widget.AppCompatImageView can't use method with RemoteViews: setImageBitmap(class android.graphics.Bitmap)

It seems to be caused by missing RemotableViewMethod annotation on the setImageBitmap() of the AppCompatImageView. Maybe it was removed in support 26+ or never existed, as AppCompatImageView is not intended for use in RemoteViews... It seems that in previous releases of the support library (23.1.0), "setImageResource(int)" and "setBackgroundResource(int)" started failing the same way: https://issuetracker.google.com/issues/37071559

What I'm trying to understand is:

  • Does it work as intended? I suppose Yes, but then:
  • How to workaround that? (probably I'm missing something...) Or:
  • Is there a way to avoid inflating AppCompatimageViews instead of simple ImageView, when using RemoteViews.apply(Context, ViewGroup) for inflation of RemoteViews? The context that is supplied to the apply method is "AppCompatActivity".

The only "solution" (an ugly workaround), I was able to find, is to retrieve the Bitmap data from the RemoteView using reflection, and then use that image in my own layout. (Instead of inflating the RemoteViews and adding everything to the layout).

Thanks in advance

ivtoto
  • 241
  • 1
  • 9
  • In principle, you could wrap your `AppCompatActivity` in a `ContextWrapper` and provide that wrapper to `apply()`, then override `getSystemService()` and have your `ContextWrapper` return a plain `LayoutInflater` rather than the AppCompat-infused `LayoutInflater` that your activity returns. I'm not quite certain how to get a plain `LayoutInflater`, though. – CommonsWare Oct 10 '17 at 17:37
  • 5
    Or, you could try passing `getApplicationContext()` as the `Context` to `apply()`, per the workarounds suggested in the issue that you cited. – CommonsWare Oct 10 '17 at 17:50
  • The application context did the job! Thanks! – ivtoto Oct 11 '17 at 08:42
  • @CommonsWare thanks getApplicationContext() solved my problem. – tech.mohit.garg Jan 07 '22 at 07:26

0 Answers0