悬浮窗口继承的是 LinearLayout,没有 Activity,如何获取一个 Activity

2020 年 4 月 23 日
 atfeel
//1 、这部分是创建一个悬浮窗口,添加到 WindowManager
this.floatView = new FloatView();
this.mWindowManager.addView(this.mElfinFloatView,this.mElfinFloatView.getLayoutParams());


//2 、悬浮窗口类继承 LinearLayout,
public class FloatView extends LinearLayout implements OnClickListener
{
......
//这里新建另一个继承 DialogFragment 的浮窗类
FragmentPage mdf = new FragmentPage();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mdf.show(ft, "df");

//getSupportFragmentManager()是活动 Activity 里才有的方法。
//但是这个类没有 Activity,怎办啊
//从其他窗口传 Activity 过来行不通,原来 Activity 销毁,传过来就没用了
......
}

谁有什么办法能解决吗??
13357 次点击
所在节点    Android
17 条回复
Lin0936
2020 年 4 月 23 日
取一下当前显示的 Activity?
BigDogWang
2020 年 4 月 23 日
换 dialog
zongren
2020 年 4 月 23 日
context 不就是 activity 吗。。。
shenhb
2020 年 4 月 23 日
你别用 dialog 相关的啊,dialog 相关必须依附于 activity 的,随便其他的 view 都行啊
atfeel
2020 年 4 月 23 日
@BigDogWang dialog 没法实现 TabLayout+ViewPager 联动
atfeel
2020 年 4 月 23 日
@shenhb 其他的 view 做不到弹窗的 view
atfeel
2020 年 4 月 23 日
@zongren context 没有 getSupportFragmentManager
asAnotherJack
2020 年 4 月 23 日
获取当前的 activity ?通过 application 的 registerLifecycle 那个很长的方法
atfeel
2020 年 4 月 23 日
@asAnotherJack 现在是根本获取不到。。
xuroid
2020 年 4 月 23 日
new FloatView()的时候加 Context 吧。
caibou
2020 年 4 月 23 日
在 application 注册 ActivityLifecycleCallbacks,可以拿到当前在 activity
Anarchy
2020 年 4 月 23 日
悬浮窗如果是需要依附在 Activity 的,直接 View 新建实例的时候穿 Activity 就好了。如果不依附 Activity 使用 Application 的话,那么你的问题就是取当前正在可见或聚焦的 Activity 是哪个了。如果是下面的问题可以通过 Application#registerActivityLifecycleCallbacks 方法保存下当前可见的 Activity
BigDogWang
2020 年 4 月 23 日
@atfeel 是吗?我记得 DialogFragment 最后还是依托于 Dialog 实现的。
如果不行的话就重新写一个悬浮 View ?
atfeel
2020 年 4 月 23 日
@xuroid 加进去也没有用吧,Context 本身是没有 getSupportFragmentManager()这个方法的,还是我弄错哪里了?
KunMinX
2020 年 4 月 24 日
可以试试 DialogActivity
atfeel
2020 年 4 月 25 日
@KunMinX DialogActivity 是基于 application 的,我现在的窗口显示在 WindowService 服务上,
bage2020
2020 年 4 月 28 日
参考 @Anarchy 开发

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/665353

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX