通过ActivityGroup与全局Handler管理Activity切换

关于ActivityGroup,对它最常见的应用就是自定义格式的标签页,用来取代灵活性不高的TabActivity。下面两个链接是很完整的ActivityGroup标签页的教程:

http://www.cnblogs.com/over140/archive/2010/09/07/1820876.html

http://blog.csdn.net/hellogv/article/details/6057174

 

ActivityGroup实质上就是在一个界面中管理多个Activity,用Android官方API文档的原话来说是:“A screen that contains and runs multiple embedded activities.”。其核心是包含这些Activity的LocalActivityManager对象,在ActivityGroup中通过getLocalActivityManager()方法获得。关于ActivityGroup源码的分析参见如下链接:

http://blog.csdn.net/caowenbin/article/details/5876019

 

简单地说,LocalActivityManager对象维护了一个Activity的哈希表,通过String标识来增删改查对应的Activity实例,并提供了对相应Activity实例操作的方法,如startActivity、destroyActivity以及其它dispatchXXXXX方法。

 

不过,在前面的ActivityGroup标签页教程示例中,标签页中的Activity都仅仅只是简单的文字Activity,并不包含能够跳转到其它Activity的按钮。也就是说,切换这些Activity的标签都是写到ActivityGroup的布局中,而没有在内嵌的Activity之中。(如农民伯伯那篇博文中的第一条回复所提的问题。)

 

关于Handler,其实就是一个消息处理队列,但是通常只是用到单独的Android组件之中。将其作为全局共享变量,就可以将不同组件间的消息统一处理了。Android全局共享变量参见如下的链接:

http://blog.csdn.net/androidbluetooth/article/details/6611138

 

下面这个示例就是将切换(或者说跳转)Activity的按钮放到了内嵌的Activity的布局中,在ActivityGroup中通过全局的Handler处理具体的Activity切换,以达到在内嵌Activity中添加跳转按钮的目的。

 

主类MainActivity,继承自ActivityGroup

package com.android.test.activitygroup;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.ViewGroup;

public class MainActivity extends ActivityGroup {
	private ViewGroup container;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        container = (ViewGroup) findViewById(R.id.container);
//        fillContainer(0);
        Message msg = new Message();
        msg.what = 0;
        mHandler.sendMessage(msg);
        
        ((SharedHandlerApp)getApplication()).setHandler(mHandler);
    }
    
    private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			fillContainer(msg.what);
		}
    	
    };
    
    private void fillContainer(int index) {
    	if (index >= 0 && index < LABELS.length) {
    		String className = "com.android.test.activitygroup." + LABELS[index] + "Activity";
    		try {
    			Intent intent = new Intent(MainActivity.this, Class.forName(className));
    			container.removeAllViews();
    			container.addView(getLocalActivityManager().startActivity(LABELS[index], intent).getDecorView());
    			
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    private static final String[] LABELS = {"First", "Second", "Third"};
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
}

3个内嵌Activity类

FirstActivity

package com.android.test.activitygroup;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class FirstActivity extends Activity implements OnClickListener {
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.child);
		
		findViewById(R.id.ahead).setOnClickListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		((TextView)findViewById(R.id.title)).setText("This is FirstActivity!");
		
		handler = ((SharedHandlerApp)getApplication()).getHandler();
	}

	public void onClick(View v) {
		Message msg = new Message();
		
		if (v.getId() == R.id.ahead) {
			msg.what = MainActivity.SECOND;
		} else if (v.getId() == R.id.back) {
			msg.what = MainActivity.THIRD;
		}
		
		handler.sendMessage(msg);
	}

} 

SecondActivity

package com.android.test.activitygroup;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class SecondActivity extends Activity implements OnClickListener {
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.child);
		
		findViewById(R.id.ahead).setOnClickListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		((TextView)findViewById(R.id.title)).setText("This is SecondActivity!");
		
		handler = ((SharedHandlerApp)getApplication()).getHandler();
	}

	public void onClick(View v) {
		Message msg = new Message();
		
		if (v.getId() == R.id.ahead) {
			msg.what = MainActivity.THIRD;
		} else if (v.getId() == R.id.back) {
			msg.what = MainActivity.FIRST;
		}
		
		handler.sendMessage(msg);
	}

}

ThirdActivity

package com.android.test.activitygroup;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class ThirdActivity extends Activity implements OnClickListener {
	private Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.child);
		
		findViewById(R.id.ahead).setOnClickListener(this);
		findViewById(R.id.back).setOnClickListener(this);
		((TextView)findViewById(R.id.title)).setText("This is ThirdActivity!");
		
		handler = ((SharedHandlerApp)getApplication()).getHandler();
	}

	public void onClick(View v) {
		Message msg = new Message();
		
		if (v.getId() == R.id.ahead) {
			msg.what = MainActivity.FIRST;
		} else if (v.getId() == R.id.back) {
			msg.what = MainActivity.SECOND;
		}
		
		handler.sendMessage(msg);
	}

}

全局变量设置类SharedHandlerApp,继承自Application

package com.android.test.activitygroup;

import android.app.Application;
import android.os.Handler;

public class SharedHandlerApp extends Application {
	private Handler handler;

	public Handler getHandler() {
		return handler;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}
}

xml布局文件

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:textColor="@android:color/white"
    android:textSize="23sp"
    />
<FrameLayout 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:layout_weight="1"
	android:background="@android:color/background_light"
	android:id="@+id/container"
	/>
<ImageView 
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:background="@android:drawable/toast_frame"
	/>
</LinearLayout>

child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <TextView 
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:textColor="@android:color/black"
    	android:textSize="23sp"
    	android:id="@+id/title"
    	android:text="Hello World!"
    	/>
    <Button 
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="ahead"
    	android:id="@+id/ahead"
    	/>
    <Button 
    	android:layout_width="wrap_content"
    	android:layout_height="wrap_content"
    	android:text="back"
    	android:id="@+id/back"
    	/>
</LinearLayout>

配置文件AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.test.activitygroup"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".SharedHandlerApp">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="FirstActivity"></activity>
        <activity android:name="SecondActivity"></activity>
        <activity android:name="ThirdActivity"></activity>

    </application>
</manifest>

AndroidManifest.xml中一定不要忘了将<application>标签的android:name属性设置为前面的扩展自Application类的全局变量设置类。


最后需要补充的是,作为container容器的Layout只要是继承自ViewGroup的Layout类即可,即LinearLayout、FrameLayout、GridLayout、ScrollLayout、AbsoluteLayout等均可,而并不局限于只能是其中的某一种。

你可能感兴趣的:(android,String,layout,application,Class,encoding)