V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
atfeel
V2EX  ›  Android

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

  •  
  •   atfeel · Apr 23, 2020 · 13635 views
    This topic created in 2207 days ago, the information mentioned may be changed or developed.
    //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 销毁,传过来就没用了
    ......
    }

    谁有什么办法能解决吗??
    17 replies    2020-04-28 20:20:25 +08:00
    Lin0936
        1
    Lin0936  
       Apr 23, 2020
    取一下当前显示的 Activity?
    BigDogWang
        2
    BigDogWang  
       Apr 23, 2020
    换 dialog
    zongren
        3
    zongren  
       Apr 23, 2020
    context 不就是 activity 吗。。。
    shenhb
        4
    shenhb  
       Apr 23, 2020
    你别用 dialog 相关的啊,dialog 相关必须依附于 activity 的,随便其他的 view 都行啊
    atfeel
        5
    atfeel  
    OP
       Apr 23, 2020
    @BigDogWang dialog 没法实现 TabLayout+ViewPager 联动
    atfeel
        6
    atfeel  
    OP
       Apr 23, 2020
    @shenhb 其他的 view 做不到弹窗的 view
    atfeel
        7
    atfeel  
    OP
       Apr 23, 2020
    @zongren context 没有 getSupportFragmentManager
    asAnotherJack
        8
    asAnotherJack  
       Apr 23, 2020
    获取当前的 activity ?通过 application 的 registerLifecycle 那个很长的方法
    atfeel
        9
    atfeel  
    OP
       Apr 23, 2020
    @asAnotherJack 现在是根本获取不到。。
    xuroid
        10
    xuroid  
       Apr 23, 2020
    new FloatView()的时候加 Context 吧。
    caibou
        11
    caibou  
       Apr 23, 2020
    在 application 注册 ActivityLifecycleCallbacks,可以拿到当前在 activity
    Anarchy
        12
    Anarchy  
       Apr 23, 2020
    悬浮窗如果是需要依附在 Activity 的,直接 View 新建实例的时候穿 Activity 就好了。如果不依附 Activity 使用 Application 的话,那么你的问题就是取当前正在可见或聚焦的 Activity 是哪个了。如果是下面的问题可以通过 Application#registerActivityLifecycleCallbacks 方法保存下当前可见的 Activity
    BigDogWang
        13
    BigDogWang  
       Apr 23, 2020
    @atfeel 是吗?我记得 DialogFragment 最后还是依托于 Dialog 实现的。
    如果不行的话就重新写一个悬浮 View ?
    atfeel
        14
    atfeel  
    OP
       Apr 23, 2020
    @xuroid 加进去也没有用吧,Context 本身是没有 getSupportFragmentManager()这个方法的,还是我弄错哪里了?
    KunMinX
        15
    KunMinX  
       Apr 24, 2020
    可以试试 DialogActivity
    atfeel
        16
    atfeel  
    OP
       Apr 25, 2020
    @KunMinX DialogActivity 是基于 application 的,我现在的窗口显示在 WindowService 服务上,
    bage2020
        17
    bage2020  
       Apr 28, 2020
    参考 @Anarchy 开发
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2961 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 07:26 · PVG 15:26 · LAX 00:26 · JFK 03:26
    ♥ Do have faith in what you're doing.