标签:
androidactivitygrouptabactiviy差异性杂谈 |
分类: Android技术 |
1)<wbr>ActivityGroup</wbr> <wbr><wbr></wbr></wbr>
<wbr><wbr><wbr><span size="+0" style="word-wrap:normal; word-break:normal; line-height:24px">根据SDK的解释其功能“<span style="color:#ED1C24; word-wrap:normal; word-break:normal"><strong>A screen that contains and runs multiple embedded activities.”。</strong><span style="color:#000000; word-wrap:normal; word-break:normal">翻译成汉语也就是大概</span></span><span style="font-weight:bold"><span style="color:#ED1C24; word-wrap:normal; word-break:normal">"<span style="word-wrap:normal; word-break:normal; line-height:normal; font-weight:normal; font-size:12px; font-family:宋体,微软雅黑,'Lucida Grande',Geneva,Arial,Verdana,'Lucida sans Unicode',Helvetica,sans-serif; color:rgb(112,112,112); border-collapse:separate"><span style="word-wrap:normal; word-break:normal; line-height:19px; font-size:13px"><strong><span style="color:#ED1C24; word-wrap:normal; word-break:normal">在Android中,ActivityGroup类是Activity的容器,可以包含多个嵌套进来的Activitys</span></strong></span></span>".<span style="word-wrap:normal; word-break:normal; line-height:normal; font-weight:normal; font-size:12px; font-family:宋体,微软雅黑,'Lucida Grande',Geneva,Arial,Verdana,'Lucida sans Unicode',Helvetica,sans-serif; color:rgb(112,112,112); border-collapse:separate"><span style="word-wrap:normal; word-break:normal; line-height:19px; font-size:13px"><span style="color:#000000; word-wrap:normal; word-break:normal">接下来依然采用源码分析的方式来了解该类的内部实现。</span></span></span></span></span></span></wbr></wbr></wbr>
<wbr><wbr><wbr><span style="color:#000000; word-wrap:normal; word-break:normal">首先,从SDK中和源码中都可以获知,ActivityGroup类的父类是Activity,也就是说二者具有相同的接口和生命周期,同Activity一样,也有onCreate()、onPause()等函数可供我们重载。在ActivityGroup的源码中有成员变量<br><wbr><wbr><wbr>protected LocalActivityManager mLocalActivityManager;<br> 该变量在ActivityGroup的构造函数中创建并初始化,可见,ActivityGroup的功能实现肯定是要委托给这个对象来完成了。为了给用户开放对此对象的访问,ActivityGroup提供了</wbr></wbr></wbr></span></wbr></wbr></wbr>
<wbr></wbr>
1 2 3 4 5 |
public
final
LocalActivityManager getLocalActivityManager
(
)
{
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>return</strong></span> mLocalActivityManager<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr>
|
通过浏览ActivityGroup的源码可以发现,几乎全部是以通过LocalActivityManager对象来完成的具体动作,比如:
1 2 3 4 5 6 7 8 9 10 11 |
protected
void
onCreate
(
Bundle savedInstanceState
)
{
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>super</strong></span>.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">onCreate</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>savedInstanceState<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
Bundle states <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> savedInstanceState <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">!=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">?</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>Bundle<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> savedInstanceState.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">getBundle</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>STATES_KEY<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">:</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
mLocalActivityManager.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">dispatchCreate</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>states<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr></wbr></wbr></wbr>
|
下面,我们就来看一下LocalActivityManager的源码。在该类中,提供了一个私有类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
private
static
class
LocalActivityRecord
extends
Binder
{
<wbr>
LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">String</span> _id, Intent _intent<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
id <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> _id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
intent <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> _intent<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><wbr><wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">String</span> id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Unique name of this record.</em></span>
<wbr>
Intent intent<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Which activity to run here.</em></span>
<wbr>
ActivityInfo activityInfo<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Package manager info about activity.</em></span>
<wbr>
Activity activity<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Currently instantiated activity.</em></span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">Window</span> window<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Activity's top-level window.</em></span>
<wbr>
Bundle instanceState<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Last retrieved freeze state.</em></span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>int</strong></span> curState <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> RESTORED<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(102,102,102); background-color:transparent"><em>// Current state the activity is in.</em></span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
|
用于保存Activity的信息,并提供了
1 2 3 4 5 6 7 |
private
final
Map
<
String, LocalActivityRecord
>
mActivities
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>new</strong></span> HashMap<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent"><</span>String, LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>private</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> ArrayList<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent"><</span>LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">></span> mActivityArray
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>new</strong></span> ArrayList<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent"><</span>LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span></wbr></wbr></wbr>
|
采用这样的数据结构用于对所有嵌入的子Activity信息进行保存处理。其中前者用于通过String快速查找,后者用于以数组的方式快速访问,是典型的以空间换时间的的方式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
public
void
dispatchCreate
(
Bundle state
)
{
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>if</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>state <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">!=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> Iterator<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent"><</span>String<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">></span> i <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> state.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">keySet</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span>.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">iterator</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>while</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>i.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">hasNext</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>try</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">String</span> id <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> i.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">next</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>final</strong></span> Bundle astate <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> state.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">getBundle</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
LocalActivityRecord r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> mActivities.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">get</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>if</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">!=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
r.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">instanceState</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> astate<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>else</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>new</strong></span> LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id, <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
r.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">instanceState</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> astate<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
mActivities.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">put</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id, r<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
mActivityArray.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">add</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>r<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>catch</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,51,153); background-color:transparent">Exception</span> e<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
……
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr><wbr><wbr>
mCurState <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> CREATED<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
|
从这里我们可以看出,当有一个ActivityGroup被Create的时候,就会有对应的Activity信息被保存到数组中。
当我们调用LocalActivityManager的startActivity()以产生Window的时候,我们也可以看到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public
Window
startActivity
(
String
id, Intent intent
)
{
<wbr>
……
<wbr>
LocalActivityRecord r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> mActivities.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">get</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>if</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">==</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>null</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
r <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>new</strong></span> LocalActivityRecord<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id, intent<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
adding <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">=</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,0,102); background-color:transparent"><strong>true</strong></span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr>
……
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; background-color:transparent"><strong>if</strong></span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>adding<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span> <span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">{</span>
<wbr>
mActivities.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">put</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>id, r<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr>
mActivityArray.<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,102,51); background-color:transparent">add</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">(</span>r<span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">)</span><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(51,153,51); background-color:transparent">;</span>
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span>
<wbr>
……
<wbr><span style="word-wrap:normal; word-break:normal; border:0px; padding:0px; margin:0px; color:rgb(0,153,0); background-color:transparent">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
|
有了这个数组,就可以遍历到ActivityGroup中嵌入的Activitys了,从而可以实现ActivityGroup的功能。
其核心的方面主要体现在:
<wbr><wbr><wbr><wbr><wbr><wbr>Intent i = new Intent(MainActivity.this,IndexActivity.class);<br><wbr><wbr><wbr><wbr><wbr><wbr>Window indexWindow = localManager.startActivity(INDEX_ID, i);<br><wbr><wbr><wbr><wbr><wbr><wbr>indexDecorView = indexWindow.getDecorView();</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr>localManager = this.getLocalActivityManager();</wbr></wbr></wbr></wbr></wbr>
然后将view加进你想要用的模块,多个view之间控制好隐藏和显示,就可以了 ,用activityGroup你会被焦点的问题搞郁闷,我现在一直纠结,如何在window 之间切换,或者request到focus,有这个方面的经验可以谈下。
范例1 使用ActivityGroup来切换Activity和Layout
<wbr><wbr><wbr></wbr></wbr></wbr>
在一个主界面中做Activity切换一般都会用TabActivity,使用方便,Activity互相之间相对独立,但是可定制性不强,而且修改起来很麻烦。当然也可以把layout分开,把逻辑代码全写在主界面的逻辑代码中,但是很明显可维护性相当差,这里通过ActivityGroup来解决这个问题。
一、效果图
要求点击底部不同图片按钮切换不同的Activity,并在中间显示Activity对应的ContentView。
<wbr></wbr>
二、 实现代码
2.1 layout.xml
< LinearLayout<wbr></wbr> xmlns:android ="http://schemas.android.com/apk/res/android"
<wbr><wbr><wbr><wbr>android:layout_width</wbr></wbr></wbr></wbr> ="fill_parent" <wbr>android:orientation</wbr> ="vertical"
<wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr> ="fill_parent" >
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> < LinearLayout<wbr></wbr> android:gravity ="center_horizontal"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:background</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="@drawable/myinfor2" <wbr>android:layout_width</wbr> ="fill_parent"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="wrap_content" >
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> < TextView<wbr></wbr> android:id ="@+id/cust_title" <wbr>android:textColor</wbr> ="@android:color/white"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:textSize</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="28sp" <wbr>android:text</wbr> ="模块1" <wbr>android:layout_width</wbr> ="wrap_content"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="wrap_content" ></ TextView >
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> </ LinearLayout >
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> <!-- <wbr>中间动态加载View<wbr></wbr></wbr> -->
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> < ScrollView<wbr></wbr> android:measureAllChildren ="true" <wbr>android:id</wbr> ="@+id/containerBody"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_weight</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="1" <wbr>android:layout_height</wbr> ="fill_parent"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_width</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="fill_parent" >
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> </ ScrollView >
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> < LinearLayout<wbr></wbr> android:background ="@android:color/black"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_gravity</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="bottom" <wbr>android:orientation</wbr> ="horizontal"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_width</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="fill_parent" <wbr>android:layout_height</wbr> ="wrap_content" >
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <!-- <wbr>功能模块按钮1<wbr></wbr></wbr> -->
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> < ImageView<wbr></wbr> android:id ="@+id/btnModule1" <wbr>android:src</wbr> ="@android:drawable/ic_dialog_dialer"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginLeft</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="7dp" <wbr>android:layout_marginTop</wbr> ="3dp"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginBottom</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="3dp" <wbr>android:layout_width</wbr> ="wrap_content"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="wrap_content" <wbr></wbr> />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <!-- <wbr>功能模块按钮2<wbr></wbr></wbr> -->
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> < ImageView<wbr></wbr> android:id ="@+id/btnModule2" <wbr>android:src</wbr> ="@android:drawable/ic_dialog_info"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginLeft</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="7dp" <wbr>android:layout_marginTop</wbr> ="3dp"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginBottom</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="3dp" <wbr>android:layout_width</wbr> ="wrap_content"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="wrap_content" <wbr></wbr> />
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> <!-- <wbr>功能模块按钮3<wbr></wbr></wbr> -->
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> < ImageView<wbr></wbr> android:id ="@+id/btnModule3" <wbr>android:src</wbr> ="@android:drawable/ic_dialog_alert"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginLeft</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="7dp" <wbr>android:layout_marginTop</wbr> ="3dp"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_marginBottom</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="3dp" <wbr>android:layout_width</wbr> ="wrap_content"
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>android:layout_height</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> ="wrap_content" <wbr></wbr> />
<wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr> </ LinearLayout >
</ LinearLayout >
2.2 TestView.java
public <wbr></wbr> class <wbr>TestView<wbr></wbr></wbr> extends <wbr>ActivityGroup<wbr>{<br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr> private <wbr>ScrollView<wbr>container<wbr></wbr></wbr></wbr> = <wbr></wbr> null ;
<wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> protected <wbr></wbr> void <wbr>onCreate(Bundle<wbr>savedInstanceState)<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> super .onCreate(savedInstanceState);
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // <wbr>隐藏标题栏</wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>requestWindowFeature(Window.FEATURE_NO_TITLE);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // <wbr>设置视图</wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>setContentView(R.layout.layout);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> = <wbr>(ScrollView)<wbr>findViewById(R.id.containerBody);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // <wbr>模块1</wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>ImageView<wbr>btnModule1<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> = <wbr>(ImageView)<wbr>findViewById(R.id.btnModule1);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>btnModule1.setOnClickListener(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> new <wbr>OnClickListener()<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> public <wbr></wbr> void <wbr>onClick(View<wbr>v)<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.removeAllViews();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.addView(getLocalActivityManager().startActivity(<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> " Module1 " ,
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> new <wbr>Intent(TestView.</wbr> this ,<wbr>ModuleView1.</wbr> class )
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.getDecorView());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>})<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // <wbr>模块2</wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>ImageView<wbr>btnModule2<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> = <wbr>(ImageView)<wbr>findViewById(R.id.btnModule2);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>btnModule2.setOnClickListener(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> new <wbr>OnClickListener()<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> public <wbr></wbr> void <wbr>onClick(View<wbr>v)<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.removeAllViews();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.addView(getLocalActivityManager().startActivity(<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> " Module2 " ,
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> new <wbr>Intent(TestView.</wbr> this ,<wbr>ModuleView2.</wbr> class )
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.getDecorView());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>});<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> // <wbr>模块3</wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>ImageView<wbr>btnModule3<wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> = <wbr>(ImageView)<wbr>findViewById(R.id.btnModule3);<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>btnModule3.setOnClickListener(</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> new <wbr>OnClickListener()<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>@Override<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> public <wbr></wbr> void <wbr>onClick(View<wbr>v)<wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.removeAllViews();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>container.addView(getLocalActivityManager().startActivity(<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> " Module3 " ,
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> new <wbr>Intent(TestView.</wbr> this ,<wbr>ModuleView3.</wbr> class )
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>.getDecorView());<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>});<br><wbr><wbr><wbr><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
代码说明:
a). ModuleView1、 ModuleView2 、 ModuleView3 分别继承自Activity。
b). 想动态改变标题可以通过cust_title获取TextView进行设置。
注释:想代码实现将子activity的所用的layout加入到主activty中的layout
public class FormActivity extends ActivityGroup {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form);
LocalActivityManager m = getLocalActivityManager();
Intent intent = new Intent().setClass(this, ContactFieldActivity.class);
Window w = m.startActivity("tratat", intent);
View v = w.getDecorView();
LinearLayout container = (LinearLayout)findViewById(R.id.fieldsContainer);
container.addView(v);
}
}
<wbr>范例<wbr>2<wbr>ActivityGroup + GridView 实现Tab分页标签</wbr></wbr></wbr>
<wbr><wbr><wbr><a href="http://blog.csdn.net/hellogv/article/details/6057174" style="text-decoration:none; color:rgb(41,113,187)">http://blog.csdn.net/hellogv/article/details/6057174</a></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr> 2) tabActivity
在一个主界面中做Activity切换一般都会用TabActivity,使用方便,Activity互相之间相对独立,但是可定制性不强,而且修改起来很麻烦。当然也可以把layout分开,把逻辑代码全写在主界面的逻辑代码中,但是很明显可维护性相当差,这里通过ActivityGroup来解决这个问题
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元