在Activity中使用LocalActivityManager

最近自己在项目时候,是这样的,我们可能很多对ViewPager并不陌生,viewPager</span><span class="pun" style="color: rgb(102, 102, 0);">大部分是加载多个</span><span class="pln" style="color: rgb(0, 0, 0);">view</span><span class="pun" style="color: rgb(102, 102, 0);">,而我这项目是</span><span class="pln" style="color: rgb(0, 0, 0);">viewpager</span><span class="pun" style="color: rgb(102, 102, 0);">加载多个</span><span class="pln" style="color: rgb(0, 0, 0);">activity</span><span class="pun" style="color: rgb(102, 102, 0);">,那么就要把</span><span class="pln" style="color: rgb(0, 0, 0);">activity</span><span class="pun" style="color: rgb(102, 102, 0);">转换成</span><span class="pln" style="color: rgb(0, 0, 0);">view</span><span class="pun" style="color: rgb(102, 102, 0);">对象,</span><span class="typ" style="color: rgb(102, 0, 102);">Android</span><span class="pun" style="color: rgb(102, 102, 0);">已经给我们封装了</span><span class="pln" style="color: rgb(0, 0, 0);">api</span><span class="pun" style="color: rgb(102, 102, 0);">,</span></p>
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="typ" style="color: rgb(102, 0, 102);">LocalActivityManager</span><span class="pun" style="color: rgb(102, 102, 0);">类是管理</span><span class="pln" style="color: rgb(0, 0, 0);">activity</span><span class="pun" style="color: rgb(102, 102, 0);">的,然后通过</span><span class="pln" style="color: rgb(0, 0, 0);">startActivity</span><span class="pun" style="color: rgb(102, 102, 0);">(</span><span class="typ" style="color: rgb(102, 0, 102);">String</span><span class="pln" style="color: rgb(0, 0, 0);"> id</span><span class="pun" style="color: rgb(102, 102, 0);">,</span><span class="typ" style="color: rgb(102, 0, 102);">Intent</span><span class="pln" style="color: rgb(0, 0, 0);"> intent</span><span class="pun" style="color: rgb(102, 102, 0);">)这个方法获取</span><span class="typ" style="color: rgb(102, 0, 102);">Window</span></p><p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pun" style="color: rgb(102, 102, 0);">获取当前</span><span class="typ" style="color: rgb(102, 0, 102);">Window</span><span class="pun" style="color: rgb(102, 102, 0);">对象,再然后调用</span><span class="pln" style="color: rgb(0, 0, 0);">getDecorView</span><span class="pun" style="color: rgb(102, 102, 0);">()方法获取当前</span><span class="pln" style="color: rgb(0, 0, 0);">activity</span><span class="pun" style="color: rgb(102, 102, 0);">对应的</span><span class="pln" style="color: rgb(0, 0, 0);">view</span><span class="pun" style="color: rgb(102, 102, 0);">,然而在添加</span><span class="pln" style="color: rgb(0, 0, 0);">view</span><span class="pun" style="color: rgb(102, 102, 0);">对象的时候会遇到这个问题</span></p>
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="typ" style="color: rgb(102, 0, 102);">Caused</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="kwd" style="color: rgb(0, 0, 136);">by</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> java</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="pln" style="color: rgb(0, 0, 0);">lang</span><span class="pun" style="color: rgb(102, 102, 0);">.</span><span class="typ" style="color: rgb(102, 0, 102);">IllegalStateException</span><span class="pun" style="color: rgb(102, 102, 0);">:</span><span class="pln" style="color: rgb(0, 0, 0);"> </span><span class="typ" style="color: rgb(102, 0, 102);">Activities</span><span class="pln" style="color: rgb(0, 0, 0);"> can</span><span class="str" style="color: rgb(0, 136, 0);">'t be added until the containing group has been created.</span></p>
<p style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px;"><span class="pun" style="color: rgb(102, 102, 0);">解决办法:</span></p><p style="line-height: 26px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; white-space: normal;"><span class="typ" style="line-height: 24.5px; color: rgb(102, 0, 102);">LocalActivityManager</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);"> mLocalActivityManager</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">;</span></p><p style="line-height: 26px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; white-space: normal;"><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">oncreate</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">()方法实例化</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">mLocalActivityManager </span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">=</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 24.5px; color: rgb(0, 0, 136);">new</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);"> </span><span class="typ" style="line-height: 24.5px; color: rgb(102, 0, 102);">LocalActivityManager</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">((</span><span class="typ" style="line-height: 24.5px; color: rgb(102, 0, 102);">Activity</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">)</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);"> context</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="line-height: 24.5px; color: rgb(0, 0, 136);">true</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">);</span></p><p style="line-height: 26px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; white-space: normal;"><span class="lit" style="line-height: 24.5px; color: rgb(0, 102, 102);">2</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">、在</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">oncreate</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">onresume</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">,</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">onpause</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">方法中分别加入如下代码:</span></p><p style="line-height: 26px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; white-space: normal;"><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">oncreate</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">()--------------</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">mLocalActivityManager</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">dispatchCreate</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">arg0</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">);参数</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">arg0</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">为</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">oncreate</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">方法的形参</span></p><p style="line-height: 26px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; white-space: normal;"><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">onpause</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">()--------------</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">mLocalActivityManager</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">dispatchPause</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">(</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">isFinishing</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">());传入</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">true</span></p><p style="line-height: 26px; margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; white-space: normal;"><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">onresume</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">()--------------</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">mLocalActivityManager</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">.</span><span class="pln" style="line-height: 24.5px; color: rgb(0, 0, 0);">dispatchResume</span><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">();</span></p><div><span class="pun" style="line-height: 24.5px; color: rgb(102, 102, 0);">
</span></div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值