1

I'm writing an application in which I'm adding a view to the WindowManager using the following code:

private final FloatingBubbleView mBtn;
FloatingBubbleView iv = new FloatingBubbleView(context);
this.mBtn = iv;
this.mWm = (WindowManager) mAppContext.get().getSystemService(Context.WINDOW_SERVICE);
mWm.addView(mBtn, mLp);

the layout params configuration:

final WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.width = iv.getLayoutParams().width;
lp.height = iv.getLayoutParams().height;
lp.type = WindowManager.LayoutParams.TYPE_TOAST;
lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
lp.format = PixelFormat.TRANSLUCENT;
lp.gravity = Gravity.TOP | Gravity.LEFT;
lp.x = 0;
lp.y = 100;

On all operation systems prior to Android 7.0 (Nougat) this view sticks to the WindowManager on switching an activity and I can still use it in the next opened activity.

On Nougat for some reason the views are removed from screen.

The question: Does anyone knows why this happens and how can I restore the previous behavior that I have on previous systems? does it has something to do with the new freeform window mode added in Nougat, described here:

Android Nougat’s freeform window mode: what it is and how developers can utilize it

halfer
  • 19,824
  • 17
  • 99
  • 186
Emil Adz
  • 40,709
  • 36
  • 140
  • 187

1 Answers1

1

I suppose the problem in TYPE_TOAST flag. I saw this bug with view disappearing on Nougat.

You can try to use TYPE_SYSTEM_ALERT flag

shmakova
  • 6,076
  • 3
  • 28
  • 44
  • First of all, thanks for you help...I will added that to do the test, but I assume that if the problem was in the permission I wouldn't be able to add the view to the WindowManager in the first place, which I can do.... the problem is that this view disappears on activity transition..am I right? – Emil Adz May 25 '17 at 13:25
  • @EmilAdz What LayoutParams do you use for your FloatingBubbleView? – shmakova May 25 '17 at 13:34
  • Added the layout params configuration to the question. – Emil Adz May 25 '17 at 13:37
  • Ok, let me try it for a second, but to make the problem clear again... the problem only exist on the last version on Naugat (7.1.1). the previous version (7.0)... everything works great. So could you please explain what you mean by: "but in last version of Android everything is ok."? – Emil Adz May 25 '17 at 13:49
  • After changing the type to the one you specified I'm getting the following error: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@e6e33e2 -- permission denied for this window type. I'm guessing it related to the permission you specified before? – Emil Adz May 25 '17 at 13:53
  • @EmilAdz you need permission as I wrote before that. https://stackoverflow.com/a/39271080/4141049 – shmakova May 25 '17 at 14:01
  • Well, first of all thank you very much for you help.It looks like this solves my problem, and the view sticks to the screen when I change screen, but in return forces the user to go through the permission screen to allow it to "Draw over the apps". Something that wasn't required when I was using the TYPE_TOAST. Is there maybe another type that will not require to turn this permission on but still work as intended? – Emil Adz May 25 '17 at 14:34